Python Selenium Chrome 网络驱动程序

标签 python selenium selenium-chromedriver

我正在开始自动化这本无聊的东西,我正在尝试通过 python 打开一个 chrome 网络浏览器。我已经安装了 selenium 和

我已尝试运行此文件:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()
browser.get('https://automatetheboringstuff.com')

但正因为如此,我得到了这个错误:

Traceback (most recent call last):   File "C:\Program Files
   (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py",
 line 74, in start
     stdout=self.log_file, stderr=self.log_file)   File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 707, in __init__
     restore_signals, start_new_session)   File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 990, in _execute_child
     startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last):   File "C:/Program Files
(x86)/Python36-32/test.py", line 5, in <module>
    browser = webdriver.Chrome()   File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py",
line 62, in __init__
   self.service.start()   File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py",
line 81, in start
   os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
  executable needs to be in PATH. Please see
https://sites.google.com/a/chromium.org/chromedriver/home

最佳答案

您需要指定您的chromedriver所在的路径

  1. Download chromedriver for your desired platform from here .

  2. 将 chromedriver 放在系统路径或代码所在的位置。

  3. 如果不使用系统路径,请链接您的 chromedriver.exe(对于非 Windows 用户,它只是称为 chromedriver):

    browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")
    

    (将 executable_path 设置为您的 chromedriver 所在的位置。)

    如果您已将 chromedriver 放置在系统路径中,则只需执行以下操作即可快捷方式:

    browser = webdriver.Chrome()

  4. 如果您运行的是基于 Unix 的操作系统,您可能需要在下载后更新 chromedriver 的权限以使其可执行:

    chmod +x chromedriver

  5. 就是这样。如果您仍然遇到问题,可以在另一篇 StackOverflow 文章中找到更多信息:Can't use chrome driver for Selenium

关于Python Selenium Chrome 网络驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42478591/

相关文章:

python - 将结尾为 "\"的字符串添加到字符串列表的所有元素

selenium - 当我们使用 Jenkins windows 服务模式执行 selenium 脚本时,从渲染器 : 600. 000 接收消息超时

python - 输入(): "NameError: name ' n' is not defined"

java - Selenium - 如何在第一次出现后从 Set 中跳过记录

java - WebDriver 驱动程序 = new ChromeDriver(options);显示构造函数 ChromeDriver(ChromeOptions) 未定义

java - 当 findElement 方法无法识别可用属性时,如何定位 WebElement?

python - Selenium WebDriverException 尝试保存屏幕截图时出错(Python 3.6)

Java Selenium 错误 : NoClassDefFoundError: org/openqa/selenium/HasAuthentication

python - 在 python 的子进程中调用 bash 时缺少别名设置

python - 代码终止后保持 matplotlib/pyplot 窗口打开