【问题标题】:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATHselenium.common.exceptions.WebDriverException:消息:“chromedriver”可执行文件需要在 PATH 中
【发布时间】:2018-02-14 22:08:48
【问题描述】:

我正在尝试使用 python 和 selenium 自动化我的 Web 应用程序,我面临以下问题。

环境 - Mac/Python/Selenium IDE - PyCharm

selenium.common.exceptions.WebDriverException:消息:'chromedriver' 可执行文件需要在 PATH 中。请参见 https://sites.google.com/a/chromium.org/chromedriver/home

请帮我解决这个问题。

【问题讨论】:

标签: python selenium pycharm selenium-chromedriver


【解决方案1】:

是的。因为您还没有通过 Selenium 驱动您的 Chrome 浏览器所需的 Chrome 二进制文件。

您需要根据您的操作系统从以下 URL 下载二进制文件:-

https://chromedriver.storage.googleapis.com/index.html?path=2.32/

使用下面的代码:-

import os
from selenium import webdriver

chromedriver = "/Users/adam/Downloads/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("http://stackoverflow.com")

更改上述代码中chromedriver的路径

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities.CHROME
cap = {'binary_location': /Users/adam/Downloads/chromedriver"}
driver = webdriver.Chrome(desired_capabilities=cap, executable_path="/Users/adam/Downloads/chromedriver")
driver.get('http://google.com/')

或者,您可以像这样使用 chromedriver 的直接路径:

 driver = webdriver.Chrome('/path/to/chromedriver')

来源:

Running Selenium WebDriver python bindings in chrome

【讨论】:

【解决方案2】:

您需要从ChromeDriver Download 页面下载chromedriver 二进制文件并将其放置在系统中的任何位置。当您启动 WebDriver 实例时,您需要提及 ChromeDriver 二进制文件的绝对路径。

在我的Windows 8 系统上,以下代码块完美运行:

from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)

【讨论】:

  • @ShubhamJain 明确提到代码块是基于Windows 8系统的。
  • 谁能帮我解决mac环境问题。
  • @HillHacker 您是否下载了 Mac 平台相关的 chromedriver 二进制版本 2.32?你能告诉我你存放它的位置吗?
【解决方案3】:

首先你需要从https://sites.google.com/a/chromium.org/chromedriver/downloads下载chrome驱动,然后解压。然后将此文件添加到环境参数中。然后写 driver = webdriver.Chrome('C:\YourPathofChromeDriver\chromedriver.exe')

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 2018-02-15
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2018-07-11
    • 2019-06-13
    相关资源
    最近更新 更多