selenium.common.exceptions.WebDriverException 报错问题解决

我的开发环境:

操作系统Mac OS 10.12
Python3.6.5
Chrome71.0
chromedriver2.45

selenium + chromedriver 可以让代码模拟人类使用浏览器访问网站的行为,但是在初始化的时候报错是令人头疼的,比如我就遇到了这样的报错内容:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

它的意思是说,找不到我的chrome浏览器的位置,所以需要给出我chrome浏览器的路径。

现在我从头到尾初始化一遍:

>>> from selenium import webdriver  #第一步导入
>>> options = webdriver.ChromeOptions()  #调用ChromeOptions方法
>>> options.binary_location = r"/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome"  
'''
给出chrome浏览器的路径,我是Mac系统;我这里是Google Chrome 2,因为我还有一个低版本的
chrome,正常情况这里应该是Google Chrome.app
'''
>>> chrome_driver_binary = "/usr/bin/chromedriver"  #给出chromedriver的路径
>>> driver = webdriver.Chrome(chrome_driver_binary,chrome_options = options) 
#初始化,通过selenium打开chrome浏览器
>>> driver.get("http://www.baidu.com")  #打开百度

这样就可以顺利通过selenium打开chrome浏览器了。

#注意:安装selenium非常简单,只需要pip3 install selenium就可以了;

安装chromedriver需要找和自己chrome浏览器版本相对应的chromedriver版本,具体可以参考这个链接:

http://chromedriver.storage.googleapis.com/2.45/notes.txt

chromedriver下载链接:http://chromedriver.storage.googleapis.com/index.html

我的chrome版本是71.0,安装的chromedriver版本是2.45的。版本必须对应才可以。

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值