我正在使用python和selenium,并从这个网站下载了用于我的windows计算机的chromedriver: http://chromedriver.storage.googleapis.com/index.html?path=2.15/

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

然而,当我运行以下代码:

  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

但是-如上所述-可执行文件在路径中(!)…这是怎么回事?


当前回答

如果你正在使用远程解释器,你还必须检查它的可执行路径是否定义。在我的例子中,从远程Docker解释器切换到本地解释器解决了这个问题。

其他回答

在最近的版本中,创建chromedriver的首选方式是使用服务。

手动设置路径,如下所示:

chromedriver_path = "path to your chromedriver executable>"

service = Service(chromedriver_path)
driver = webdriver.Chrome(service=service)

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

这可以使用webdriver-manager自动完成

pip install webdriver-manager

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

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

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

同样可以用来设置Firefox、Edge和ie二进制文件。

PIP安装webdriver-manager

如果使用python3运行脚本:

安装webdriver-manager

然后在脚本中请使用:

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

你可以测试它是否真的在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")

适用于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

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

步骤2:将chromedriver添加到/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

您现在应该可以运行了

from selenium import webdriver

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

没有任何问题