我的开发环境:
| 操作系统 | Mac OS 10.12 |
| Python | 3.6.5 |
| Chrome | 71.0 |
| chromedriver | 2.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的。版本必须对应才可以。
本文详细介绍如何在MacOS环境下,解决Selenium与ChromeDriver配合使用时遇到的找不到Chrome浏览器位置的问题。通过指定Chrome和ChromeDriver的正确路径,实现自动化测试。
1260

被折叠的 条评论
为什么被折叠?



