Python Selenium Chrome Webdriver

76
我正在开始学习《用Python自动化无聊的工作》这本书,我试图通过Python打开Chrome浏览器。我已经安装了Selenium。
我尝试运行了这个文件:

我正在开始自动化枯燥的工作这本书,并尝试通过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

3
回溯信息已经告诉了您问题所在:您的Python代码无法执行“chromedriver”,因为它在PATH中找不到。将chromedriver的位置添加到PATH中,或将chromedriver移动到已经在PATH中的位置。 - fragmentedreality
1
这个回答解决了你的问题吗?selenium - chromedriver可执行文件需要在PATH中 - robertspierre
2个回答

135

您需要指定chromedriver的路径

  1. 从这里下载适合您所用平台的chromedriver

  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()

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

    chmod +x chromedriver

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


  • 2
    或者按照错误提示所述的做法,将可执行文件放在系统PATH中的某个位置。 - Corey Goldberg
    Selenium-Driverless 是一个使用 Python 编写的替代方案。 - undefined

    45

    这里有一个更简单的解决方案:安装python-chromedriver包,在您的脚本中导入即可完成。

    步骤
    1. 使用pip安装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/


    2
    您需要执行pip install chromedriver-binary。 - Prometheus
    @Prometheus 是的,我在第一步中提到了这一点。 - Louis
    5
    对我来说这并不奏效,因为pip install chromedriver_binary会安装最新版本(84),但我的Chrome版本是83。所以我必须运行pip install chromedriver-binary==83.0.4103.39。您可以在此处找到其他版本:https://sites.google.com/a/chromium.org/chromedriver/。 - JesusAlvSoto
    1
    非常简单,它不起作用。 - Ozan Kurt
    2
    这个不再起作用了。请参见:https://dev59.com/O1IG5IYBdhLWcg3whQaH#70099102 - JayRizzo
    pip install chromedriver-binary-auto - undefined

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接