简体   繁体   English

Selenium Python browser = webdriver.Firefox()错误

[英]Selenium Python browser=webdriver.Firefox() error

I'm trying out selenium's Simple Usage . 我正在尝试硒的简单用法 Its code driver = webdriver.Firefox() gave an error. 其代码driver = webdriver.Firefox()给出了错误。

This is my full code: 这是我的完整代码:

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

and the result: 结果:

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..')

The Selenium import success! Selenium import success! result indicates that there is no problem during importing. 结果表明导入期间没有问题。 On the other hand, browser = webdriver.Firefox() is the one causing problem. 另一方面, browser = webdriver.Firefox()是引起问题的一个原因。

How should I change it to make it work? 我应该如何对其进行更改以使其起作用? Note: The file I am working on it called auto.py 注意:我正在处理的文件名为auto.py

Download the Windows version of Geckodriver from here and place it somewhere convenient. 此处下载Windows版的Geckodriver,并将其放在方便的地方。 Then when initializing the browser variable, pass the full path to geckodriver.exe like so: 然后在初始化browser变量时,将完整路径传递给geckodriver.exe如下所示:

browser = webdriver.Firefox(executable_path='enter_path_here')

the error message states the issue clearly: "'geckodriver' executable needs to be in PATH". 错误消息明确指出了该问题: "'geckodriver' executable needs to be in PATH".

You must download geckodriver executable and make sure it is located on your PATH. 您必须下载geckodriver可执行文件,并确保它位于PATH中。

Place your geckodriver like this : 像这样放置您的geckodriver:

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  

Note that D:/Automation/geckodriver.exe this should be geckodriver path. 请注意, D:/Automation/geckodriver.exe应该是geckodriver路径。 Just for your simplicity use I have written this. 为了您的简单起见,我已经写了这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM