记录一下Unbuntu安装selenium + Python3 + chromedrive环境遇到的困难
Chromedrive
从http://chromedriver.storage.googleapis.com/index.html选择对应版本进行安装。
安装到Download目录之后,解压.复制到bin目录下
sudo cp -r chromedrive /usr/bin
Python3
Unbuntu 自带Python3 版本,可以查询Python 版本
python3 --version
Selenium
sudo pip install selenium
提示已经安装了,估计unbuntu已经自带了.
python3
>>import selenium
>>help(selenium)
编辑了一个Py文件进行测试
# -*- coding: utf-8 -*-
from selenium import webdriver
help(selenium)
driver = webdriver.Firefox()
driver.get('www.baidu.com')
driver.quit()
发现类似下面错误
Traceback (most recent call last):
File “/home/yanner/seleniumtest/sousuo.py”, line 2, in
browser = webdriver.Firefox()
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py”, line 164, in init
self.service.start()
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py”, line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.
于是参考网上解决办法:
ubuntu系统,使用selenium3.X版本的,火狐浏览器需要下载geckodriver:
https://github.com/mozilla/geckodriver/releases
下载后直接解压,将解压后的文件geckodriver放入/usr/local/bin目录下(该目录是存放执行文件的)
打开终端输入以下命令:
su root (如出现su认证失败请点击看上一篇文章)
然后进入文件geckodriver存放的目录,将该文件复制到/usr/local/bin目录下
root@yanner-VirtualBox:/home/yanner# cd ruanjian
root@yanner-VirtualBox:/home/yanner/ruanjian# cp geckodriver /usr/local/bin/
root@yanner-VirtualBox:/home/yanner/ruanjian#
再次运行:
from selenium import webdriver
browser = webdriver.Firefox()
不再提示:‘geckodriver’ executable needs to be in PATH.
成功解决,环境配置完毕.
Chrome Driver
下载了对应的Chrome 的Chrome Driver 之后,编辑测试脚本,发现报错
看样子是Chrome Driver 和 Chrome的版本不对应,但是看了网站是对应版本的.
想着使用一下pycharm,在Pycharm上装Selenium 包,提示需要安装包,执行提示语句:pip install --user xxxxx(提示安装语句)
发现报错,然后再试一次…结果又安装成功
所以改了一下测试代码,指定下Chrome Driver 的URL:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
chromedriver = "/usr/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path=chromedriver)
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("Selenium2")
driver.find_element_by_id("su").click()
driver.quit()
成功!
用回原来的测试代码,又成了…有点神奇