错误消息:“’chromedriver’可执行文件必须在路径中可用”

问题:错误消息:“’chromedriver’可执行文件必须在路径中可用”

我正在将硒与python结合使用,并已从以下站点下载了适用于Windows计算机的chromedriver:http ://chromedriver.storage.googleapis.com/index.html?path=2.15 /

下载zip文件后,我将zip文件解压缩到我的下载文件夹中。然后,我将可执行二进制文件(C:\ Users \ michael \ Downloads \ chromedriver_win32)的路径放入环境变量“路径”中。

但是,当我运行以下代码时:

  from selenium import webdriver

  driver = webdriver.Chrome()

…我不断收到以下错误消息:

WebDriverException: Message: 'chromedriver' executable needs to be available in the path. Please look at     http://docs.seleniumhq.org/download/#thirdPartyDrivers and read up at http://code.google.com/p/selenium/wiki/ChromeDriver

但是-如上所述-可执行文件在路径中是(!)…这里发生了什么?

I am using selenium with python and have downloaded the chromedriver for my windows computer from this site: http://chromedriver.storage.googleapis.com/index.html?path=2.15/

After downloading the zip file, I unpacked the zip file to my downloads folder. Then I put the path to the executable binary (C:\Users\michael\Downloads\chromedriver_win32) into the Environment Variable “Path”.

However, when I run the following code:

  from selenium import webdriver

  driver = webdriver.Chrome()

… I keep getting the following error message:

WebDriverException: Message: 'chromedriver' executable needs to be available in the path. Please look at     http://docs.seleniumhq.org/download/#thirdPartyDrivers and read up at http://code.google.com/p/selenium/wiki/ChromeDriver

But – as explained above – the executable is(!) in the path … what is going on here?


回答 0

您可以测试它是否确实在PATH中,如果您打开cmd并输入chromedriver(假设您的chromedriver可执行文件仍以此命名),然后按Enter。如果Starting ChromeDriver 2.15.322448显示,则PATH设置正确,并且还有其他问题。

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

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

因此,在您的特定情况下:

 driver = webdriver.Chrome("C:/Users/michael/Downloads/chromedriver_win32/chromedriver.exe")

You can test if it actually is in the PATH, if you open a cmd and type in chromedriver (assuming your chromedriver executable is still named like this) and hit Enter. If Starting ChromeDriver 2.15.322448 is appearing, the PATH is set appropriately and there is something else going wrong.

Alternatively you can use a direct path to the chromedriver like this:

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

So in your specific case:

 driver = webdriver.Chrome("C:/Users/michael/Downloads/chromedriver_win32/chromedriver.exe")

回答 1

我看到讨论仍在讨论通过下载二进制文件并手动配置路径来设置chromedriver的旧方法。

可以使用webdriver-manager自动完成

pip install webdriver-manager

现在,问题中的上述代码将可以在下面的更改中简单地工作,

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

可以使用相同的方法来设置Firefox,Edge和二进制文件。

I see the discussions still talk about the old way of setting up chromedriver by downloading the binary and configuring the path manually.

This can be done automatically using webdriver-manager

pip install webdriver-manager

Now the above code in the question will work simply with below change,

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

The same can be used to set Firefox, Edge and ie binaries.


回答 2

pycharm社区版的情况与此相同,因此对于cmd,必须重新启动ide才能重新加载路径变量。重新启动您的ide,应该没问题。

Same situation with pycharm community edition, so, as for cmd, you must restart your ide in order to reload path variables. Restart your ide and it should be fine.


回答 3

在Linux(Ubuntu或Debian)上:

sudo apt install chromium-chromedriver

在macOS上安装https://brew.sh/然后执行

brew cask install chromedriver

On Ubuntu:

sudo apt install chromium-chromedriver

On Debian:

sudo apt install chromium-driver

On macOS install https://brew.sh/ then do

brew cask install chromedriver

回答 4

r对于原始字符串,我们必须添加路径字符串,以字符串之前的字母开头。我以这种方式进行了测试,并且有效。

driver = webdriver.Chrome(r"C:/Users/michael/Downloads/chromedriver_win32/chromedriver.exe")

We have to add path string, begin with the letter r before the string, for raw string. I tested this way, and it works.

driver = webdriver.Chrome(r"C:/Users/michael/Downloads/chromedriver_win32/chromedriver.exe")

回答 5

一些额外的输入/说明,供以后使用该线程的读者使用,以避免修改PATH env。Windows级别的变量并重新启动Windows系统:(从https://stackoverflow.com/a/49851498/9083077复制我的答案,适用于Chrome):

(1)下载chromedriver(如本主题前面所述),然后将(解压缩的)chromedriver.exe放在X:\ Folder \ of \ your \ choice中

(2)Python代码示例:

import os;
os.environ["PATH"] += os.pathsep + r'X:\Folder\of\your\choice';

from selenium import webdriver;
browser = webdriver.Chrome();
browser.get('http://localhost:8000')
assert 'Django' in browser.title

注意:(1)示例代码(在引用的答案中)可能需要5秒钟打开Firefox浏览器以获取指定的URL。(2)如果尚无服务器在指定的url上运行或提供标题为字符串’Django’的页面,则python控制台将显示以下错误:在browser.title AssertionError中断言’Django’。

Some additional input/clarification for future readers of this thread, to avoid tinkering with the PATH env. variable at the Windows level and restart of the Windows system: (copy of my answer from https://stackoverflow.com/a/49851498/9083077 as applicable to Chrome):

(1) Download chromedriver (as described in this thread earlier) and place the (unzipped) chromedriver.exe at X:\Folder\of\your\choice

(2) Python code sample:

import os;
os.environ["PATH"] += os.pathsep + r'X:\Folder\of\your\choice';

from selenium import webdriver;
browser = webdriver.Chrome();
browser.get('http://localhost:8000')
assert 'Django' in browser.title

Notes: (1) It may take about 5 seconds for the sample code (in the referenced answer) to open up the Firefox browser for the specified url. (2) The python console would show the following error if there’s no server already running at the specified url or serving a page with the title containing the string ‘Django’: assert ‘Django’ in browser.title AssertionError


回答 6

对于Linux和OSX

步骤1:下载chromedriver

# You can find more recent/older versions at http://chromedriver.storage.googleapis.com/
# Also make sure to pick the right driver, based on your Operating System
wget http://chromedriver.storage.googleapis.com/81.0.4044.69/chromedriver_mac64.zip

第2步:将chromedriver添加到 /usr/local/bin

unzip chromedriver_mac64.zip
cp chromedriver /usr/local/bin

您现在应该可以运行

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://localhost:8000')

没有任何问题

For Linux and OSX

Step 1: Download chromedriver

# You can find more recent/older versions at http://chromedriver.storage.googleapis.com/
# Also make sure to pick the right driver, based on your Operating System
wget http://chromedriver.storage.googleapis.com/81.0.4044.69/chromedriver_mac64.zip

For debian: wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip

Step 2: Add chromedriver to /usr/local/bin

unzip chromedriver_mac64.zip
sudo mv chromedriver /usr/local/bin
sudo chown root:root /usr/local/bin/chromedriver
sudo chmod +x /usr/local/bin/chromedriver

You should now be able to run

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://localhost:8000')

without any issues


回答 7

解压缩chromedriver时,请务必指定确切位置,以便以后进行跟踪。在下面,您将为您的操作系统找到合适的chromedriver,然后将其解压缩到一个确切的位置,稍后可以在代码中将其作为参数提供。

wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux64.zip unzip chromedriver_linux64.zip -d /home/virtualenv/python2.7.9/

When you unzip chromedriver, please do specify an exact location so that you can trace it later. Below, you are getting the right chromedriver for your OS, and then unzipping it to an exact location, which could be provided as argument later on in your code.

wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux64.zip unzip chromedriver_linux64.zip -d /home/virtualenv/python2.7.9/


回答 8

如果您正在使用机器人框架RIDE。然后,您可以Chromedriver.exe从其官方网站下载并将此.exe文件保存在C:\Python27\Scripts目录中。现在将此路径作为环境变量提及。C:\Python27\Scripts\chromedriver.exe

重新启动计算机,然后再次运行相同的测试用例。您不会再遇到此问题。

If you are working with robot framework RIDE. Then you can download Chromedriver.exe from its official website and keep this .exe file in C:\Python27\Scripts directory. Now mention this path as your environment variable eg. C:\Python27\Scripts\chromedriver.exe.

Restart your computer and run same test case again. You will not get this problem again.


回答 9

根据说明,您需要在实例化webdriver时包括ChromeDriver的路径。Chrome例如:

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

According to the instruction, you need to include the path to ChromeDriver when instantiating webdriver.Chrome eg.:

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

回答 10

在将chromedriver添加到路径之前,请确保它与浏览器的版本相同。

如果不是,则需要匹配版本:更新/降级chrome,以及升级/降级webdriver。

我建议您尽可能多地更新Chrome版本,并匹配网络驱动程序。

要更新Chrome:

  • 在右上角,单击三个点。
  • 点击help->About Google Chrome
  • 更新版本并重新启动chrome

然后从此处下载兼容版本:http : //chromedriver.chromium.org/downloads

注意:最新的chromedriver并不总是与最新版本的chrome匹配!

现在,您可以将其添加到PATH中:

  1. 在您计算机的某个位置创建一个新文件夹,您将在其中放置Web驱动程序。我创建了一个命名的文件夹webdriversC:\Program Files

  2. 复制文件夹路径。就我而言C:\Program Files\webdrivers

  3. 右键单击this PC-> properties

  1. 在右键上 Advanced System settings
  2. 请点击 Environment Variables
  3. 在中System variables,单击,path然后单击edit
  4. 点击 new
  5. 粘贴之前复制的路径
  6. 在所有窗口上单击确定

而已!我使用了pycharm,不得不重新打开它。也许与其他IDE或终端相同。

Before you add the chromedriver to your path, make sure it’s the same version as your browser.

If not, you will need to match versions: either update/downgrade you chrome, and upgrade/downgrade your webdriver.

I recommend updating your chrome version as much as possible, and the matching the webdriver.

To update chrome:

  • On the top right corner, click on the three dots.
  • click help -> About Google Chrome
  • update the version and restart chrome

Then download the compatible version from here: http://chromedriver.chromium.org/downloads .

Note: The newest chromedriver doesn’t always match the newest version of chrome!

Now you can add it to the PATH:

  1. create a new folder somewhere in your computer, where you will place your web drivers. I created a folder named webdrivers in C:\Program Files

  2. copy the folder path. In my case it was C:\Program Files\webdrivers

  3. right click on this PC -> properties:

  1. On the right click Advanced System settings
  2. Click Environment Variables
  3. In System variables, click on path and click edit
  4. click new
  5. paste the path you copied before
  6. click OK on all the windows

Thats it! I used pycharm and I had to reopen it. Maybe its the same with other IDEs or terminals.


回答 11

如果您完全确定PATH设置正确,可以尝试重新启动计算机,如果它无法正常工作。

就Windows 7而言,我总是在WebDriverException上出现错误:消息:对于chromedriver,gecodriver,IEDriverServer。我很确定我有正确的路径。重启电脑,一切正常

Could try to restart computer if it doesn’t work after you are quite sure that PATH is set correctly.

In my case on windows 7, I always got the error on WebDriverException: Message: for chromedriver, gecodriver, IEDriverServer. I am pretty sure that i have correct path. Restart computer, all work


回答 12

就我而言,当我将chromedriver文件复制到c:\ Windows文件夹时,此错误消失了。这是因为Windows目录位于python脚本检查chromedriver可用性的路径中。

In my case, this error disappears when I have copied chromedriver file to c:\Windows folder. Its because windows directory is in the path which python script check for chromedriver availability.


回答 13

如果使用远程解释器,则还必须检查是否定义了其可执行文件PATH。在我的情况下,从远程Docker解释器切换到本地解释器解决了问题。

If you are using remote interpreter you have to also check if its executable PATH is defined. In my case switching from remote Docker interpreter to local interpreter solved the problem.


回答 14

我遇到了与您相同的问题。我正在使用PyCharm编写程序,我认为问题出在PyCharm中而不是OS中。我解决了该问题,方法是进行脚本配置,然后手动编辑环境变量中的PATH。希望对您有所帮助!

I encountered the same problem as yours. I’m using PyCharm to write programs, and I think the problem lies in environment setup in PyCharm rather than the OS. I solved the problem by going to script configuration and then editing the PATH in environment variables manually. Hope you find this helpful!


回答 15

C:\ Windows处添加webdriver(chromedriver.exe或geckodriver.exe)。这对我来说很有效

Add the webdriver(chromedriver.exe or geckodriver.exe) here C:\Windows. This worked in my case


回答 16

最好的方法可能是获取当前目录并将剩余地址附加到该目录。像这样的代码(Windows上的Word。在Linux上,您可以使用pwd行): webdriveraddress = str(os.popen("cd").read().replace("\n", ''))+'\path\to\webdriver'

The best way is maybe to get the current directory and append the remaining address to it. Like this code(Word on windows. On linux you can use something line pwd): webdriveraddress = str(os.popen("cd").read().replace("\n", ''))+'\path\to\webdriver'


回答 17

当我下载chromedriver.exe时,我只是将其移动到PATH文件夹C:\ Windows \ System32 \ chromedriver.exe中,却遇到了完全相同的问题。

对我来说,解决方案是只更改PATH中的文件夹,因此我将其移到了PATH中也位于Pycharm Community bin文件夹中。例如:

  • C:\ Windows \ System32 \ chromedriver.exe->给我exceptions
  • C:\ Program Files \ JetBrains \ PyCharm Community Edition 2019.1.3 \ bin \ chromedriver.exe->运行正常

When I downloaded chromedriver.exe I just move it in PATH folder C:\Windows\System32\chromedriver.exe and had exact same problem.

For me solution was to just change folder in PATH, so I just moved it at Pycharm Community bin folder that was also in PATH. ex:

  • C:\Windows\System32\chromedriver.exe –> Gave me exception
  • C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\bin\chromedriver.exe –> worked fine

回答 18

Mac Mojave运行机器人测试框架和Chrome 77时出现了此问题。这解决了问题。感谢@Navarasu将我指向正确的轨道。

$ pip install webdriver-manager --user # install webdriver-manager lib for python
$ python # open python prompt

接下来,在python提示符下:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

# ctrl+d to exit

这导致以下错误:

Checking for mac64 chromedriver:xx.x.xxxx.xx in cache
There is no cached driver. Downloading new one...
Trying to download new driver from http://chromedriver.storage.googleapis.com/xx.x.xxxx.xx/chromedriver_mac64.zip
...
TypeError: makedirs() got an unexpected keyword argument 'exist_ok'
  • 我现在得到了最新的下载链接
    • 将chromedriver下载并解压缩到所需位置
    • 例如: ~/chromedriver/chromedriver

~/.bash_profile用编辑器打开并添加:

export PATH="$HOME/chromedriver:$PATH"

打开新的终端窗口,ta-da🎉

Had this issue with Mac Mojave running Robot test framework and Chrome 77. This solved the problem. Kudos @Navarasu for pointing me to the right track.

$ pip install webdriver-manager --user # install webdriver-manager lib for python
$ python # open python prompt

Next, in python prompt:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

# ctrl+d to exit

This leads to the following error:

Checking for mac64 chromedriver:xx.x.xxxx.xx in cache
There is no cached driver. Downloading new one...
Trying to download new driver from http://chromedriver.storage.googleapis.com/xx.x.xxxx.xx/chromedriver_mac64.zip
...
TypeError: makedirs() got an unexpected keyword argument 'exist_ok'
  • I now got the newest download link
    • Download and unzip chromedriver to where you want
    • For example: ~/chromedriver/chromedriver

Open ~/.bash_profile with editor and add:

export PATH="$HOME/chromedriver:$PATH"

Open new terminal window, ta-da 🎉


回答 19

我在Webdriver 3.8.0(Chrome 73.0.3683.103和ChromeDriver 73.0.3683.68)上遇到了此问题。我做完之后问题就消失了

pip install -U selenium

将Webdriver升级到3.14.1。

I had this problem on Webdriver 3.8.0 (Chrome 73.0.3683.103 and ChromeDriver 73.0.3683.68). The problem disappeared after I did

pip install -U selenium

to upgrade Webdriver to 3.14.1.


回答 20

最好的确定方法是在这里:

下载并解压缩chromedriver并将chromedriver.exe放入C:\ Python27 \ Scripts中,然后您无需提供驱动程序的路径,只需

driver= webdriver.Chrome()

您无需添加路径或其他任何操作

Best way for sure is here:

Download and unzip chromedriver and put ‘chromedriver.exe’ in C:\Python27\Scripts and then you need not to provide the path of driver, just

driver= webdriver.Chrome()

You are done no need to add paths or anything


回答 21

检查您的Chrome驱动程序的路径,它可能无法从那里获取。只需复制即可将驱动程序位置粘贴到代码中。

Check the path of your chrome driver, it might not get it from there. Simply Copy paste the driver location into the code.


回答 22

(对于Mac用户)我有同样的问题,但是我通过以下简单方法解决了:您必须将chromedriver.exe放在执行脚本的同一文件夹中,然后在pyhton中编写以下指令:

导入操作系统

os.environ [“ PATH”] + = os.pathsep + r’X:/您的/文件夹/脚本/’

(for Mac users) I have the same problem but i solved by this simple way: You have to put your chromedriver.exe in the same folder to your executed script and than in pyhton write this instruction :

import os

os.environ[“PATH”] += os.pathsep + r’X:/your/folder/script/’