简体   繁体   English

为 Windows - selenium.common.exceptions.WebDriverException 安装 Chromedriver:消息:“chromedriver.exe”可执行文件需要在 PATH 中

[英]Installing Chromedriver for Windows - selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH

I'm trying to webscrape with Python and selenium, and I've run into an error like so:我正在尝试使用 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

It seems like I haven't installed chromedriver for selenium properly.好像我还没有为 selenium 正确安装 chromedriver。 How do I install chromedriver properly for selenium?如何为 selenium 正确安装 chromedriver?

There are a few ways you can go about this:您可以通过以下几种方法 go 关于此:

Using Chocolatey使用巧克力

The easiest way I've found to install chromedriver is to use chocolatey .我发现安装 chromedriver 的最简单方法是使用Chocolatey You can follow the instructions to install it here , and once it's installed, just run choco install chromedriver (as an administrator), and it should install chromedriver for your version of chrome.您可以按照说明在此处安装它,安装后,只需运行choco install chromedriver (以管理员身份),它应该会为您的 chrome 版本安装 chromedriver。

Then in your code, just remove the reference to chromedriver's path:然后在您的代码中,只需删除对 chromedriver 路径的引用:

Before:前:

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

After:后:

driver = webdriver.Chrome()

Manually手动

If you don't want to use chocolatey, then follow these steps如果您不想使用巧克力,请按照以下步骤操作

  1. Go to chrome://version/ in Chrome, to check your current version. Go 到 Chrome 中的chrome://version/ ,以检查您当前的版本。 As you can see it's version 89 for me:如您所见,它对我来说是89版:

版本

  1. Go to chromedriver.chromium.org/downloads and download the version of chromedriver that's the same as your browser version: Go 到chromedriver.chromium.org/downloads并下载与您的浏览器版本相同的 chromedriver 版本:

铬驱动程序

  1. Download the right version for your OS.为您的操作系统下载正确的版本。 For example, if you are using Windows, download the win_32 one:例如,如果您使用的是 Windows,请下载win_32一个:

下载

  1. Extract the.ZIP and place chromedriver.exe in the same folder as your Python program:解压 .ZIP 并将chromedriver.exe放在与 Python 程序相同的文件夹中:

  1. Change the path to chromedriver to just chromedriver.exe :将 chromedriver 的路径更改为chromedriver.exe

Before:前:

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

After:后:

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

...and you should be good to go. ...你应该对 go 很好。


Adding chromedriver to the PATH将 chromedriver 添加到 PATH

If you want to add chromedriver to the PATH so you don't have to worry about where chromedriver is each time you write a selenium program, then it'd be a good idea to just use chocolatey, because it should install it globally.如果您想将 chromedriver 添加到 PATH 中,这样您每次编写 selenium 程序时不必担心 chromedriver 在哪里,那么最好只使用巧克力,因为它应该全局安装。 It's also much easier, so go with that if you can.它也容易得多,所以如果可以的话,go 就可以了。 However, you can still set chromedriver to the PATH manually, following these steps (for Windows):但是,您仍然可以按照以下步骤(对于 Windows)手动将 chromedriver 设置为 PATH:

  1. Open Command Prompt.打开命令提示符。 Create a directory at C:\bin by running通过运行在C:\bin创建一个目录
cd /

...then: ...然后:

mkdir bin

...then: ...然后:

cd bin

...then: ...然后:

explorer .

This should open the folder in File Explorer:这应该在文件资源管理器中打开文件夹:

垃圾桶

  1. Place chromedriver.exe in this folder将 chromedriver.exe 放在此文件夹中
  2. Set it to the PATH by running, in Command Prompt:通过在命令提示符中运行将其设置为 PATH:
setx PATH "%PATH%;C:\bin"

You should get something like this:你应该得到这样的东西:

完毕

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

If so, you're all done.如果是这样,你就完成了。

Instructions for adding to PATH were adapted from here .添加到 PATH 的说明改编自此处 For other operating systems, see here .对于其他操作系统,请参见此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 selenium.common.exceptions.WebDriverException:消息:“chromedriver”可执行文件需要在 PATH 中 - selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH Selenium WebDriverException:消息:“ chromedriver.exe”可执行文件必须位于PATH中 - Selenium WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH selenium.common.exceptions.WebDriverException:消息:“chromedriver”可执行文件需要在无头 Chrome 的 PATH 错误中 - selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with Headless Chrome selenium.common.exceptions.WebDriverException:消息:无法连接到服务。/drivers/chromedriver.exe - selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service ./drivers/chromedriver.exe selenium.common.exceptions.WebDriverException:消息:打开chrome浏览器时无法连接到服务chromedriver.exe - selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser Selenium WebDriverException 'chromedriver.exe' 需要在 PATH 中 - Selenium WebDriverException 'chromedriver.exe' needs to be in PATH Gitlab CI 出现错误 selenium.common.exceptions.WebDriverException:消息:当我运行我的简单 pro 时,'chromedriver' 可执行文件需要在 PATH 中 - Gitlab CI got error selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH when I running my simple pro selenium.common.exceptions.WebDriverException:消息:服务 chromedriver 意外退出 - selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited selenium.common.exceptions.WebDriverException:消息:“库”可执行文件可能对 ChromeDriver 具有错误的权限 - selenium.common.exceptions.WebDriverException: Message: 'library' executable may have wrong permissions for ChromeDriver 我收到消息“chromedriver.exe”可执行文件需要在 PATH 中 - i got message 'chromedriver.exe' executable needs to be in PATH
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM