python - 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 的新手,我不确定下一步该做什么。任何指导将不胜感激。
解决方案
webdriver.Chrome()
如果您已经在 PATHS 中提供了正确的文件夹,则您提供的路径应该是可执行文件的路径,或者什么都不是。
然后你用方法get
访问你要抓取的url
driver = webdriver.Chrome(executable_path="path/to/chromedriver")
driver.get('https://secure.consumerreports.org/ec/login')
推荐阅读
- android - 如何将图像放在 ListView 中 TextView 行的底部
- sql - 基于多个孩子配对的父母人数
- bash - 将一个号码引用到另一个号码
- sql-server - SQL 将多行分组为 2 行
- checkbox - 如何在角度 5 的 ngFor 的给定列表中一次只选择一个复选框?
- c++ - 编译器错误:只需要复制省略,但似乎需要移动构造函数(编译器)
- perl - 如何从 perl 脚本调用 shell 脚本
- sql - ORA-02292: 违反完整性约束 (XXX.JOB_EXEC_PARAMS_FK) - 在 Spring Batch 中找到子记录
- sql - 从 2008 年到 2014 年的数据库 SQL 服务器迁移
- php - 在 Swift 4 中实现 php serialize()