Python 错误消息:“'chromedriver' 可执行文件需要在路径中可用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29858752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:06:35  来源:igfitidea点击:

Error message: "'chromedriver' executable needs to be available in the path"

pythonseleniumselenium-chromedriver

提问by steady_progress

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/

我在 python 中使用 selenium,并从这个站点下载了我的 Windows 计算机的 chromedriver: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".

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

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?

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

采纳答案by lmNt

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.322448is appearing, the PATH is set appropriately and there is something else going wrong.

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

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

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

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

So in your specific case:

所以在你的具体情况下:

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

回答by Andrea Bisello

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.

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

回答by Harshdeep Singh

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.

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

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

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

回答by learningBunny

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

如果在您非常确定 PATH 设置正确后它不起作用,可以尝试重新启动计算机。

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

就我在 Windows 7 上的情况而言,我总是在 WebDriverException: Message: for chromedriver, gecodriver, IEDriverServer 上遇到错误。我很确定我有正确的路径。重启电脑,一切正常

回答by James0007

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

r对于原始字符串,我们必须添加路径字符串,以字符串前的字母开头。我用这种方式测试过,它有效。

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

回答by Rahul Tiwari

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

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

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

重新启动计算机并再次运行相同的测试用例。你不会再遇到这个问题。

回答by Snidhi Sofpro

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/9083077as applicable to Chrome):

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

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

(1) 下载 chromedriver(如前文所述)并将(解压后的)chromedriver.exe 放在 X:\Folder\of\your\choice

(2) Python code sample:

(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

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

注意:(1)示例代码(在参考答案中)可能需要大约 5 秒才能打开指定 url 的 Firefox 浏览器。(2) 如果没有服务器已经在指定的 url 运行或提供标题包含字符串 'Django' 的页面,python 控制台将显示以下错误: assert 'Django' in browser.title AssertionError

回答by Hamid

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.

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

回答by AnnaSummerShine

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

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

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

回答by walid_data

(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 :

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

import os

导入操作系统

os.environ["PATH"] += os.pathsep + r'X:/your/folder/script/'

os.environ["PATH"] += os.pathsep + r'X:/your/folder/script/'