在Mac上使用chromedriver和selenium

15

我想在Mac上使用selenium和chromedriver,但我遇到了一些问题。

  1. 我从ChromeDriver - WebDriver for Chrome下载了chromedriver。
  2. 但我不想将它放到PATH中,所以我这样做。

enter image description here

import os

from selenium import webdriver

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "bin/chromedriver_for_mac")
print DRIVER_BIN
browser = webdriver.Chrome(DRIVER_BIN)
browser.get('http://www.baidu.com/')

但是我无法得到我想要的结果。

Traceback (most recent call last):
  File "/Users/wyx/project/python-scraping/se/test.py", line 15, in <module>
    browser = webdriver.Chrome(DRIVER_BIN)
  File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver_for_mac' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x107f96150>> ignored
  1. 然后我运行 brew cask install chromedriver。并且我只运行它而不需要指定驱动程序路径。

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

但这也行不通。

Traceback (most recent call last):
  File "/Users/wyx/project/python-scraping/se/test.py", line 16, in <module>
    browser = webdriver.Chrome()
  File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x105c08150>> ignored

Process finished with exit code 1

最后我试着把它放到/usr/bin目录下

➜  Downloads sudo cp chromedriver /usr/bin
Password:
cp: /usr/bin/chromedriver: Operation not permitted

我尝试在.zshrc中使用export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac,但是出现了以下错误:

selenium.common.exceptions.WebDriverException: Message: 'chromedriver'可执行文件需要在PATH中。

那么如何解决呢?我想使用不在PATH中的驱动程序路径,以便可以轻松部署我的项目。

解决方案:

  1. brew cask install chromedriver
  2. which chromedriver 获取驱动程序路径
  3. 然后像这样使用:webdriver.Chrome("/usr/local/bin/chromedriver")

但我不知道为什么要使用selenium如此复杂。


截至2023年,brew install chromedriver --cask 的安装位置为 /opt/homebrew/bin/chromedriver - james-see
4个回答

14
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' 可执行文件需要在 PATH 中。
使用 ChromeDriver 启动 Chrome 浏览器,你需要将 可执行的 chromedriver 位置与可执行文件本身一起传递到 executable_path 中。
你可以尝试以下操作:
import os
from selenium import webdriver

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "bin/chromedriver_for_mac")

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

或者将 PATH 变量 使用带可执行文件的命令 设置为:

export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac

然后尝试将ChromeDriver初始化为:

from selenium import webdriver

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

要启动ChromeDriver,您需要传递可执行文件所在的可执行文件目录,而不仅仅是目录。请注意。 - Saurabh Gaur
chromedriver_for_mac 不是一个目录,好吗?我的截图清楚地显示了这一点。 - wyx
哦,那是我的错误,但你必须将它作为webdriver.Chrome(executable_path = DRIVER_BIN)传递给executable_path - Saurabh Gaur
将此可执行文件从当前位置移动到其他新的空目录位置,然后尝试。 - Saurabh Gaur
1
实际上,您正在将chromedriver的名称修改为chromedriver_for_mac,这就是为什么在通过brew安装时@wyx找不到它的原因。在这种情况下,您只需初始化为webdriver.Chrome(),它本身会从bin文件夹中找到chromedriver可执行文件,但它应该命名为chromedriver.. :) - Saurabh Gaur
显示剩余4条评论

8

为了简单起见:

从这个链接下载Chrome WebDriver。将“chromedriver”复制到Python脚本的文件夹中。

from selenium import webdriver
import os

url = 'http://www.webscrapingfordatascience.com/complexjavascript/'

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "chromedriver")

driver = webdriver.Chrome(executable_path = DRIVER_BIN)

driver.get(url)

input('Press ENTER to close the automated browser')

driver.quit()

7

对我来说,不需要复杂的东西就能这样做

  1. 从官方链接(注意版本与Chrome浏览器相符)下载chromedriver
  2. 解压*.zip文件,并将chromedriver文件复制到usr/local/bin/位置
  3. 删除您在文件中放置的任何路径,只需使用driver = webdriver.Chrome()
  4. 如果问题仍然存在,请尝试重新打开PyCharm,因为有时需要重新打开才能工作

一个需要在最后提醒的步骤:如果没有完成,请允许在您的Mac的安全与隐私菜单中使用chromedriver。 - Kirumosama

3

对我而言,在MacOS 11上通过Homebrew安装chromedriver非常顺利。

brew install chromedriver && brew update 

这对我也起作用了。另外,请记住苹果可能会阻止此应用程序启动,请记得从“安全性与隐私”中解除其阻止。 - Mahesh Jamdade
1
截至2023年,命令为brew install chromedriver --cask,位置为/opt/homebrew/bin/chromedriver - james-see

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接