2017-02-27 197 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 –