解决使用selenium自动控制浏览器找不到Chromedriver
最近学习爬虫过程中使用了selenium模块通过调用Chromedriver来实现自动控制Chrome,但其中遇到一些问题,在此总结。
首先,下载ChromeDriver时一定要对应好自己的浏览器版本,下载链接:http://npm.taobao.org/mirrors/chromedriver/
![cc80c9b1c76a8dcca3854047ac76a4f1.png](https://i-blog.csdnimg.cn/blog_migrate/e67a085cb370fbc8733ddc8d1258114e.jpeg)
将下载好的ChromeDriver保存至Chrome浏览器的安装文件夹下
![7b57dbe19f5c564339c6583284d3ccc4.png](https://i-blog.csdnimg.cn/blog_migrate/479bcb524102112bce97cdfabc2bcae4.jpeg)
然后将ChromeDriver的路径配置到环境变量Path中,此时正常情况下调用ChromeDriver应该可以正常使用,
from selenium import webdriverbrowser = webdriver.Chrome()browser.get('http://www.baidu.com/')
或者
from selenium import webdriveroptions = 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 webdriverbrowser = webdriver.Chrome('你的Chromedriver路径')browser.get('http://www.baidu.com/')
或
from selenium import webdriveroptions = 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/')
完美解决
![56e880601514c28692b26365a93a102d.png](https://i-blog.csdnimg.cn/blog_migrate/fdb3733084eb4621a4fe141cc1c75f93.jpeg)
![490fa58706fd1438a0805243847c866a.png](https://i-blog.csdnimg.cn/blog_migrate/cceb6fc24f5bd54e42c4d28a30f10c41.jpeg)