Selenium Python browser = webdriver.Firefox()错误

时间:2018-06-22 15:41:35

标签: python selenium selenium-webdriver

我正在尝试硒的Simple Usage。其代码 authentificateUser(email, password) : Observable<HttpResponse<any>> { return this.http.get<any>('/api/LogIn/Authentification?email=' + email + '&password=' + password, { observe: 'response' }); } 给出了错误。

这是我的完整代码:

driver = webdriver.Firefox()

和结果:

while True:
    try:
        from selenium import webdriver
        from selenium.webdriver.common.keys import Keys
    except:
        print('Failed to import selenium tools, retrying...')
        continue
    else:
        print('Selenium import success!')
        break

while True:
    try:
        browser = webdriver.Firefox()
    except:
        print('Open browser error: An error occured, retrying..')
        continue
    else:
        print('Success!')
        break

browser.get('http://www.python.org')
assert "Python" in browser.title

elem = browser.find_element_by_name('q')
elem.send_keys('pycon')
elem.send_keys(Keys.RETURN)

assert "No results found" not in browser.page_source

Selenium import success! Open browser error: An error occured, retrying..Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start stdin=PIPE) File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, 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 "D:\code\python project\auto.py", line 14, in <module> browser = webdriver.Firefox() File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 160, in __init__ self.service.start() File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\code\python project\auto.py", line 16, in <module> print('Open browser error: An error occured, retrying..') 结果表明导入过程中没有问题。另一方面,Selenium import success!是导致问题的原因。

如何更改它才能使其正常工作? 注意:我正在处理的文件名为auto.py

3 个答案:

答案 0 :(得分:1)

here下载Windows版本的Geckodriver,并将其放在方便的地方。然后,在初始化geckodriver.exe变量时,将完整路径传递给browser = webdriver.Firefox(executable_path='enter_path_here') ,如下所示:

Events==41

答案 1 :(得分:0)

错误消息清楚地指出了问题:"'geckodriver' executable needs to be in PATH".

您必须下载geckodriver可执行文件,并确保它位于您的PATH中。

答案 2 :(得分:0)

像这样放置壁虎:

while True:
    try:
        browser = webdriver.Firefox(executable_path = r'D:/Automation/geckodriver.exe')
    except:
        print('Open browser error: An error occured, retrying..')
        continue
    else:
        print('Success!')
        break  

请注意,D:/Automation/geckodriver.exe应该是geckodriver路径。为了您的简单起见,我已经写了这个。