2017-07-24 401 views
1

我有一個Python腳本,以前工作正常,但現在錯誤,所以我不知道發生了什麼。我得到的錯誤:FileNotFoundError:[WinError 2]系統找不到指定的文件,雖然以前的工作完全相同文件

C:\Users\663255\Desktop>PMI_Tests.py 
Traceback (most recent call last): 
    File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start 
    stdout=self.log_file, stderr=self.log_file) 
    File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__ 
    restore_signals, start_new_session) 
    File "C:\Users\663255\AppData\Local\Programs\Python\Python36\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:\Users\663255\Desktop\PMI_Tests.py", line 14, in <module> 
    driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe') 
    File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__ 
    self.service.start() 
    File "C:\Users\663255\AppData\Local\Programs\Python\Python36\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.exe' executable needs to be in PATH. 

的文件的開頭是這樣的:

import unittest 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import os 
import time 
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 
from selenium.webdriver.common.action_chains import ActionChains 
from urllib.request import urlopen 
from html.parser import HTMLParser 


gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver')) 
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe') 
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe') 


class PythonOrgSearch(unittest.TestCase): 

#sets up driver to run tests 
    def setUp(self): 
     self.driver = driver 

我不確定爲什麼文件停止工作,因爲它曾經工作過以前很多次。另外,python和geckodriver都是在路徑中定義的,但是當通過終端運行時,它說geckodriver不在路徑中。

我有一種感覺,問題與geckodriver(即gecko變量)在代碼中以奇怪的方式定義的方式有關,或類似的東西。我查看過類似問題的其他堆棧溢出帖子,但沒有找到任何解決我的問題的東西。如果這有幫助,我正在使用Python 3.6.2。任何見解都會很棒。謝謝!

+1

如果您使用的是Windows,您可以簡單地通過環境路徑添加系統路徑。我有同樣的問題。你能解決嗎?我在虛擬環境中使用Anaconda。 Chrome完美運作。傳統上將驅動程序放入Anaconda文件夾通常會解決,然而geckodriver不會爲我使用這個。儘管我正在使用無頭firefox。 https://github.com/kybu/headless-selenium-for-win。在你的情況下,最有可能添加驅動程序到Python目錄+更新Windows環境變量應解決 – Tetora

+0

我沒有使用anaconda。最終重新定義了gecko變量來硬編碼它的路徑,最終解決了問題。 –

回答

0

作爲此問題的解決方案,我硬編碼gecko變量中的geckodriver的完整路徑,而不是試圖動態定位它。我不確定爲什麼動態方法停止工作,但對文件路徑進行硬編碼可以解決任何問題。

相關問題