【问题标题】:Chrome Driver Needs to be available in the path error on MacChrome 驱动程序需要在 Mac 上的路径错误中可用
【发布时间】:2015-04-03 03:41:40
【问题描述】:

我在笔记本电脑上运行 OS X 10.9.4,使用 Chrome 40.0.2214.94 和 Selenium 2.44.0,我使用 easy_install 为 Python 2.7 安装了它们。我的代码是 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

我可以做些什么来解决这个问题并开始按照我的计划从某些页面检索数据?如果您可以非常明确地了解代码更改或位置更改,因为我不是最精通计算的!

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

【问题讨论】:

    标签: python-2.7 selenium selenium-chromedriver


    【解决方案1】:

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

    如错误信息所示,您需要从这里获取二进制文件:

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

    然后将二进制文件放在 PATH 中的某个位置。或者,您可以通过设置系统属性告诉 WebDriver 二进制文件在哪里。我会先将二进制文件放在 PATH 中的某个位置,如果需要,我会变得更具体/更复杂。

    /usr/bin 位于 OS X 的全局路径上,因此这是放置文件的好地方。另外,计算机上的任何用户都可以使用它。您可以通过打开 Finder 窗口来打开该文件夹,在菜单中选择 Go -> Go To Folder,然后输入 /usr/bin(可能会要求您输入密码,因为它是系统位置。)然后只需将 ChromeDriver 二进制文件复制到那里。

    【讨论】:

    • 从 El Capitan (OS X 10.11) 开始,由于“系统完整性保护”,无法再将文件添加到系统路径之一。这意味着不可能将 chromedriver 放在全局路径中。因此,我们需要更改 eclipse 中使用的全局路径。改变 bash_profile 是不够的。有人对此有解决方案吗?
    【解决方案2】:

    使用“/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://stackoverflow.com/users/login')
    

    【讨论】:

    • 我将驱动程序移至/usr/local/bin/,它可以工作!
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多