首页 > 解决方案 > Python Selenium 错误:“WebDriverException:'login' 可执行文件需要在 PATH 中。”

问题描述

我需要使用 Selenium 从网站上抓取数据,并且在 pip 安装 selenium 并将 chrome 驱动程序添加到我的 PATH 后,出现错误。这是我的代码:

from selenium import webdriver

driver = webdriver.Chrome('https://secure.consumerreports.org/ec/login')

username = driver.find_element_by_id("signin-username")
password = driver.find_element_by_id("signin-password")

username.send_keys("myname@university.edu")
password.send_keys("pa$$w0rd")

driver.find_element_by_id('signin-btn').click()

print('Login complete.')

我得到:

WebDriverException: 'login' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

为了确保,我通过终端 pip 安装了 selenium,这是成功的,我下载了 mac 的 chrome 驱动程序(https://sites.google.com/a/chromium.org/chromedriver/downloads),解压缩它,并将驱动程序本身粘贴到我的路径中。我进入echo $PATH终端,它打印:/Users/[my name]/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin. 我不允许将驱动程序粘贴到/usr/bin/bin/usr/sbin/sbin中,但其他路径都很好。

根据错误消息,似乎我必须下载某种“登录”驱动程序才能登录。我一直未能找到这样的驱动程序。

我是 Selenium 的新手,我不确定下一步该做什么。任何指导将不胜感激。

标签: pythonseleniumweb-scrapingselenium-chromedriver

解决方案


webdriver.Chrome()如果您已经在 PATHS 中提供了正确的文件夹,则您提供的路径应该是可执行文件的路径,或者什么都不是。

然后你用方法get访问你要抓取的url

driver = webdriver.Chrome(executable_path="path/to/chromedriver")
driver.get('https://secure.consumerreports.org/ec/login')

推荐阅读