我正在使用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

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


当前回答

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

没有任何问题

其他回答

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

wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux64.zip unzip余下

适用于mac osx用户

    brew tap homebrew/cask
    brew cask install chromedriver

测试后检查ChromeDriver是否已安装

chromedriver

你应该看到

Starting ChromeDriver version.number 
ChromeDriver was successful

检查ChromeDriver的路径

which chromedriver

在代码中使用Path

...
from selenium import webdriver

options = Options()
options.headless = True
options.add_argument('windows-size=1920x1080')

path   = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(path, options=options)

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

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

chromedriver_path = "path to your chromedriver executable>"

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

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