2017-02-27 161 views
2

我开始自动化无聊的东西书,我试图通过python打开Chrome浏览器。我已经安装了硒和Python Selenium Chrome Webdriver

我试图运行这个文件:

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

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:/Program Files (x86)/Python36-32/test.py", line 5, in 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

+0

追溯已经告诉你,问题是:你的Python代码不能执行'chromedriver',因为它不能在PATH中找到。将'chromedriver'的位置添加到PATH或将'chromedriver'移动到PATH中的某个位置。 – fragmentedreality

回答

7

您需要,其中指定路径的铬驱动程序位于

  1. Download Chrome Drive of your desired platform from here

  2. 将文件放在代码所在的目录或无论您想要的位置。

  3. 链接你的chromedriver.exe通过下面的代码:

    browser = webdriver.Chrome(executable_path=r"chromedriver.exe") 
    

    或更改executable_path到任何你已经存储在您的chromedriver。

  4. 你完成了!这应该做到这一点。

希望它有帮助!评论你是否还有任何疑问。

+0

谢谢我得到它的工作!真的很感谢帮助 –

+0

如果您认为它对您有帮助,请将答案标记为“最佳答案”:) –

+0

或者仅执行错误消息所述的内容并将可执行文件放在系统的某个位置PATH –