一尘不染

Python Selenium Chrome Webdriver

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

阅读 562

收藏
2020-06-26

共1个答案

一尘不染

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

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

chmod +x chromedriver

2020-06-26