Chrome 驱动程序需要在路径错误中可用 Mac

Chrome Driver Needs to be available in the path error on Mac

我在我的笔记本电脑上 运行ning OS X 10.9.4,Chrome 40.0.2214.94 和我为 Python 安装的 Selenium 2.44.0 2.7 easy_install。我的代码是 selenium 演练中的基本代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

但是当我 运行 这时我得到一个例外:

Traceback (most recent call last):
  File "/Users/masongardner/Desktop/Selenium_tester.py", line 17, in <module>
driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
  File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/chrome/webdriver.py", line 59, in __init__
self.service.start()
  File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/chrome/service.py", line 66, in start
    "ChromeDriver executable needs to be available in the path. "
selenium.common.exceptions.WebDriverException: Message: ChromeDriver executable needs to be available in the path. Please download from http://chromedriver.storage.googleapis.com/index.html and read up at http://code.google.com/p/selenium/wiki/ChromeDriver

如文档所述,chrome 在我机器上的这个位置:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome

我该怎么做才能解决这个问题并按照我的计划开始从某些页面检索数据?如果你能把代码更改或位置更改非常明确,因为我不是最精通计算的!

谢谢大家,希望我的问题不要太简单!

ChromeDriver 是一个二进制文件,将 WebDriver 与 Chrome 连接起来。如果您安装了 WebDriver 的库,并且安装了 Chrome,您仍然需要将 ChromeDriver 二进制文件放在 WebDriver 可以找到的位置。

如错误消息所示,您需要从此处获取二进制文件:

http://chromedriver.storage.googleapis.com/index.html

然后将二进制文件放在您的 PATH 中的某个位置。或者,您可以通过设置系统 属性 告诉 WebDriver 二进制文件在哪里。我会先将二进制文件放在您的 PATH 上的某个位置,然后在需要时获得更多 specific/complex。

/usr/bin 在 OS X 的全局路径上,所以这是放置文件的好地方。另外,计算机上的任何用户都可以使用它。您可以通过打开 Finder window 打开该文件夹,在菜单中选择前往 -> 前往文件夹,然后输入 /usr/bin(您可能会被要求输入密码,因为它是一个系统位置。)然后只需将 Chrome 驱动程序二进制文件复制到那里。

要在 Mac OXS El Capitan 10.11.6 上解决此问题,我已将 chromedriver 可执行文件的副本添加到:

/Library/Python/2.7/site-packages/selenium/webdriver

使用“/usr/local/bin/chromedriver”。这对我有用:

import selenium
import os

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = '/usr/local/bin/chromedriver'
browser = webdriver.Chrome(chromedriver)
browser.get('https://whosebug.com/users/login')