ChromeDriver安装与配置 使用selenium自动控制浏览器找不到Chromedriver问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

ChromeDriver安装与配置 使用selenium自动控制浏览器找不到Chromedriver问题

weixin_42508908   2021-04-21 我要评论

ChromeDriver 是 google 为网站开发人员提供的自动化测试接口,它是 selenium2 和 chrome浏览器 进行通信的桥梁。selenium 通过一套协议(JsonWireProtocol :https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol)和 ChromeDriver 进行通信,selenium 实质上是对这套协议的底层封装,同时提供外部 WebDriver 的上层调用类库。

下面看下解决使用selenium自动控制浏览器找不到Chromedriver,具体内容如下:

最近学习爬虫过程中使用了selenium模块通过调用Chromedriver来实现自动控制Chrome,但其中遇到一些问题,在此总结。

首先,下载ChromeDriver时一定要对应好自己的浏览器版本,下载链接:http://npm.taobao.org/mirrors/chromedriver/

版本对应

将下载好的ChromeDriver保存至Chrome浏览器的安装文件夹下

在这里插入图片描述

然后将ChromeDriver的路径配置到环境变量Path中,此时正常情况下调用ChromeDriver应该可以正常使用,

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')

或者

from selenium import webdriver

options = webdriver.ChromeOptions()
#options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36"')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.baidu.com/')

但有的电脑即使配置完环境变量依旧不能正常使用,看了其他博主所写,有的建议将ChromeDriver放置在python的安装目录以及工作目录下,结果是依然不起作用,产生报错:WebDriverException: Message: unknown error: cannot find Chrome binary那就只能采用设置路径的方法进行调用,如下:

from selenium import webdriver
browser = webdriver.Chrome('你的Chromedriver路径')
browser.get('http://www.baidu.com/')

from selenium import webdriver
options = webdriver.ChromeOptions()
#options.binary_location = r'你的Chrome安装路径'
#options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36"')
driver = webdriver.Chrome(chrome_options=options,executable_path=r'你的Chromedriver路径')
driver.get('https://www.baidu.com/')

在这里插入图片描述

总结

以上所述是小编给大家介绍的解决使用selenium自动控制浏览器找不到Chromedriver问题,希望对大家有所帮助,也非常感谢大家对网站的支持!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们