2017-07-24 404 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的完整路径,而不是试图动态定位它。我不确定为什么动态方法停止工作,但对文件路径进行硬编码可以解决任何问题。

相关问题