开发环境
操作系统 | Mac OS 10.13.6 |
Python | 3.7 |
Chrome | 72.0 |
chromedriver | 2.45 |
使用selenium库下webdriver模块打开谷歌浏览器时报错,代码如下:
from selenium import webdriver
# 创建浏览器对象
driver = webdriver.Chrome() # Chrome是谷歌浏览器(驱动:chromedriver),也可以更改其他浏览器注:更换其他浏览器时,需要安装浏览器相对应的驱动
driver.get('https://www.baidu.com') # 打开百度网页
driver.close() # 关闭浏览器
错误信息(1):
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
chrome浏览器版本相对应的chromedriver版本,参考链接:
https://chromedriver.storage.googleapis.com/2.46/notes.txt
下载chromedriver驱动连接:
https://chromedriver.storage.googleapis.com/index.html
chromedriver驱动复制"/usr/local/bin"目录下
,chromedriver驱动文件路径需要添加到系统配置文件中
vim ~/.bash_profile
点 i 键进入编辑模式,进行编辑(必须是英文下的大写字母或者小写字母)
添加文件路径:export PATH=$PATH:/usr/local/bin/chromedriver
编辑完成后,点esc 退出
输入" :wq! ",这四个字符后,回车即可保存
运行上述代码打开Google浏览器
Firefox配置步骤与Google浏览器相同,Geckodriver驱动下载地址:
https://github.com/mozilla/geckodriver/releases
错误信息(2):
selenium.common.exceptions.WebDriverException: Message: Host header or origin header is specified and is not localhost——指定了主机报头或源报头,而不是本地主机。
解决方法:
下载浏览器所兼容的chromedriver驱动
错误信息(3):
selenium.common.exceptions.WebDriverException:Message: Service chromedriver unexpectedly exited. Status code was: 126:chrome服务意外退出,状态码是:126
解决方案:
最新版本谷歌浏览器没有所兼容的chromedriver驱动,需要更换googlechrome版本,googlechrome版本必须兼容chromedriver驱动
查看版本信息 ⋮——>帮助——>关于Google Chrome