700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > [Mac] selenium打开Chrome浏览器

[Mac] selenium打开Chrome浏览器

时间:2021-06-20 21:53:57

相关推荐

[Mac] selenium打开Chrome浏览器

在Mac电脑用python+selenium打开Chrome还遇到几个问题,这里总结下,以防忘记,也可帮到别人。

前提:已安装好python3,selenium

安装chromedriver

1.先查看Chrome的版本,我这里是87

2. 下载与Chrome浏览器对应的chromedriver版本,下载链接:

/a//chromedriver/downloads

3. 把下载的文件解压,把chromedriver.exe拷到/usr/local/bin/下。

打开Finder,Ctrl+Shift+G, 输入/usr/local/bin/ 回车,进入到该目录把chromedriver.exe复制进来

4.添加chromedriver.exe到环境变量打开命令行终端,输入vim ~/.bash_profile添加一行:export PATH=$PATH:/usr/local/bin/chromedriver,保存并退出执行source ~/.bash_profile,让修改生效

代码

from selenium import webdriverdriver = webdriver.Chrome()driver.get("")

如果你的Chrome安装在默认路径,这时运行代码没有问题。但是我运行的时候报错:

mon.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

研究发现是我的Chrome没有安装在默认路径下,所以需要在代码里指明Chrome的路径,这里也可以再指明一次chromedriver.exe的路径,代码如下:

from selenium import webdriveroptions = webdriver.ChromeOptions()options.binary_location='/Google Chrome.app/Contents/MacOS/Google Chrome' # Chrome pathchrome_driver_binary = "/usr/local/bin/chromedriver" # chromedriver pathdriver = webdriver.Chrome(chrome_driver_binary,chrome_options=options)driver.maximize_window()driver.get("")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。