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

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


当前回答

根据指令,当实例化webdriver时,你需要包含到ChromeDriver的路径。如铬。

driver = webdriver.Chrome('/path/to/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')

没有任何问题

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

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

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

我遇到了和你一样的问题。 我使用PyCharm来编写程序,我认为问题在于PyCharm中的环境设置,而不是操作系统。 我通过转到脚本配置,然后手动编辑环境变量中的PATH解决了这个问题。 希望这对你有帮助!

可执行路径已弃用!

如果你得到exectuable_path ash been deprecated警告,下面是修复…

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
def test_me(my_name):
    s=Service(ChromeDriverManager().install())    
    chrome_driver = webdriver.Chrome(service=s)
    ...