【问题标题】:Python Selenium Chrome Webdriver [duplicate]Python Selenium Chrome Webdriver [重复]
【发布时间】:2017-07-17 15:27:02
【问题描述】:

我正在开始自动化这本无聊的东西,我正在尝试通过 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

【问题讨论】:

  • 回溯已经告诉你,问题是什么:你的python代码无法执行chromedriver,因为它在PATH中找不到。将 chromedriver 的位置添加到 PATH 或将 chromedriver 移动到 PATH 中已经存在的位置。

标签: python selenium selenium-chromedriver


【解决方案1】:

您需要指定您的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

【讨论】:

  • 或者只是按照错误消息中的说明进行操作,然后将可执行文件放在系统路径中的某个位置
【解决方案2】:

这里有一个更简单的解决方案:安装 python-chromedrive 包,在脚本中导入它,就完成了。

一步一步
1. pip install chromedriver-binary
2. 导入包

from selenium import webdriver
import chromedriver_binary  # Adds chromedriver binary to path

driver = webdriver.Chrome()
driver.get("http://www.python.org")

参考:https://pypi.org/project/chromedriver-binary/

【讨论】:

  • 你需要执行 pip install chromedriver-binary
  • @Prometheus 是的,我在步骤 1 中提到过
  • 这对我不起作用,因为 pip install chromedriver_binary 安装了最新版本(84),但我的 chrome 版本是 83。所以我不得不运行 pip install chromedriver-binary==83.0.4103.39。你可以在这里找到其他版本:sites.google.com/a/chromium.org/chromedriver
  • 很简单,不行
猜你喜欢
  • 2021-05-27
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
相关资源
最近更新 更多