2016-12-06 34 views
0

我正在嘗試使用selenium包連接到web瀏覽器的基本python腳本。顯示的錯誤日誌如下所示。使用python-selenium包時出錯

Traceback (most recent call last): 
    File "F:\WinPython\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start 
    stdout=self.log_file, stderr=self.log_file) 
    File "F:\WinPython\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\subprocess.py", line 947, in __init__ 
    restore_signals, start_new_session) 
    File "F:\WinPython\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\subprocess.py", line 1224, in _execute_child 
    startupinfo) 
FileNotFoundError: [WinError 2] The system cannot find the file specified 

在處理上述異常,另一個異常:

Traceback (most recent call last): 
    File "F:/WinPython/WinPython-64bit-3.5.2.3Qt5/python-3.5.2.amd64/py_codes/python_org_search.py", line 4, in <module> 
    driver = webdriver.Firefox() 
    File "F:\WinPython\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 140, in __init__ 
    self.service.start() 
    File "F:\WinPython\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\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: 'geckodriver' executable needs to be in PATH. 
+0

異常日誌不足以理解問題。也顯示你的代碼 – Andersson

+0

重複http://stackoverflow.com/questions/40208051/selenium-using-python-geckodriver-executable-needs-to-be-in-path已經有了答案 – CJC

+0

@Andersson這裏是代碼from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get(「http://www.python.org」) 在driver.title中斷言「Python」 ELEM = driver.find_element_by_name( 「q」) elem.clear() elem.send_keys( 「PYCON」) elem.send_keys(Keys.RETURN) 斷言 「未找到結果」。不在driver.page_source driver.close() – Anjishnu

回答

0

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.,很顯然,你需要添加geckodriver到系統路徑,更具體包含geckodriver.exe應的文件夾被添加到系統路徑。

要做到這一點,運行cmd.exe以管理員身份運行以下命令:

setx path "%path%;C:\Path\to\geckodriver.exe\;" 

與您的實際路徑替換C:\Path\to\geckodriver.exe\到包含geckodriver.exe

編輯的文件夾:

您可以從這裏下載exe文件https://github.com/mozilla/geckodriver/releases,將它解壓到某處,比如說C:\geckodriver。現在,您有這樣的文件,如C:\geckodriver\geckodriver.exe,您需要將C:\geckodriver添加到您的系統路徑。然後,在新的控制檯中運行腳本。

+0

沒有geckodriver.exe我發現的所有文件都是一個名爲geckodriver的txt文件,它生成在包含代碼的python文件所在的同一個文件夾中@ettanany – Anjishnu

+0

看一看在我編輯的答案中,如果你以前沒有下載並提取exe文件,那麼你需要按照上面的鏈接進行操作。 – ettanany