Python Selenium Chrome Webdriver

Fer*_*ldi 28 python selenium selenium-chromedriver

我开始自动化无聊的东西书,我正在尝试通过python打开一个chrome web浏览器.我已经安装了硒和

我试过运行这个文件:

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')
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

在处理上述异常期间,发生了另一个异常:

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
Run Code Online (Sandbox Code Playgroud)

Ahm*_*aha 49

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

  1. 从这里下载chromedriver为您所需的平台.

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

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

    browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")
    
    Run Code Online (Sandbox Code Playgroud)

    (设置executable_path为chromedriver所在的位置.)

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

    browser = webdriver.Chrome()

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

    chmod +x chromedriver

  5. 就这样.如果您仍然遇到问题,可以在此其他StackOverflow文章中找到更多信息:无法使用Chrome驱动程序进行Selenium

  • 或者只是执行错误消息中所述的操作,然后将可执行文件放在系统PATH中的某个位置 (2认同)

Lou*_*uis 20

这是一个更简单的解决方案:安装 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")
Run Code Online (Sandbox Code Playgroud)

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

  • 您需要执行 pip install chromedriver-binary (2认同)
  • 这对我不起作用,因为 pip install chromedriver_binary 安装最新版本(84),但我的 chrome 版本是 83。所以我必须运行 pip install chromedriver-binary==83.0.4103.39。您可以在这里找到其他版本:https://sites.google.com/a/chromium.org/chromedriver/ (2认同)
  • 这不再有效。请参阅:/sf/answers/4906937171/ (2认同)

归档时间:

查看次数:

86935 次

最近记录:

6 年,5 月 前