【问题标题】:Installing Chromedriver for Windows - selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH为 Windows 安装 Chromedriver - selenium.common.exceptions.WebDriverException:消息:“chromedriver.exe”可执行文件需要在 PATH 中
【发布时间】:2021-06-16 09:08:09
【问题描述】:

我正在尝试使用 Python 和 selenium 进行网络爬虫,但遇到了如下错误:

Traceback (most recent call last):
  File "C:/Users/You/your_code.py", line 5, in <module>
    driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')
  File "C:\Users\You\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Users\You\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'executable.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

似乎我没有正确安装 chromedriver for selenium。如何为 selenium 正确安装 chromedriver?

【问题讨论】:

    标签: python windows selenium selenium-chromedriver chocolatey


    【解决方案1】:

    有几种方法可以解决这个问题:

    使用巧克力

    我发现安装 chromedriver 的最简单方法是使用 chocolatey。您可以按照说明安装它here,安装后,只需运行choco install chromedriver(以管理员身份),它应该会为您的 chrome 版本安装 chromedriver。

    然后在您的代码中,只需删除对 chromedriver 路径的引用即可:

    之前:

    driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')
    

    之后:

    driver = webdriver.Chrome()
    

    手动

    如果您不想使用巧克力,请按照以下步骤操作

    1. 在 Chrome 中转到 chrome://version/,检查您当前的版本。如您所见,我的版本是89

    1. 前往chromedriver.chromium.org/downloads,下载与浏览器版本相同的chromedriver版本:

    1. 为您的操作系统下载正确的版本。例如,如果您使用的是 Windows,请下载win_32 一个:

    1. 解压 .ZIP 并将 chromedriver.exe 放在与 Python 程序相同的文件夹中:

    1. 将 chromedriver 的路径更改为 chromedriver.exe

    之前:

    driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')
    

    之后:

    driver = webdriver.Chrome(executable_path='chromedriver.exe')
    

    ...你应该很高兴。


    将 chromedriver 添加到 PATH

    如果您想将 chromedriver 添加到 PATH 中,这样您就不必担心每次编写 selenium 程序时 chromedriver 在哪里,那么最好只使用 Chocolatey,因为它应该安装它全球范围内。它也容易得多,所以如果可以的话,那就去吧。但是,您仍然可以按照以下步骤手动将 chromedriver 设置为 PATH(适用于 Windows):

    1. 打开命令提示符。通过运行在C:\bin 创建一个目录
    cd /
    

    ...然后:

    mkdir bin
    

    ...然后:

    cd bin
    

    ...然后:

    explorer .
    

    这应该会在文件资源管理器中打开文件夹:

    1. 将 chromedriver.exe 放在此文件夹中
    2. 通过在命令提示符中运行将其设置为 PATH:
    setx PATH "%PATH%;C:\bin"
    

    你应该得到这样的东西:

    1. 关闭并重新打开命令提示符
    2. 通过运行chromedriver -v 验证设置。你应该得到这样的东西:
    C:\Users\You>chromedriver -v
    ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761})
    

    如果是这样,你就完成了。

    添加到 PATH 的说明改编自 here。对于其他操作系统,请参阅here

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 2018-02-15
      • 2019-05-28
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      相关资源
      最近更新 更多