繁体   English   中英

Selenium Python browser = webdriver.Firefox()错误

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

我正在尝试硒的简单用法 其代码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! 结果表明导入期间没有问题。 另一方面, browser = webdriver.Firefox()是引起问题的一个原因。

我应该如何对其进行更改以使其起作用? 注意:我正在处理的文件名为auto.py

此处下载Windows版的Geckodriver,并将其放在方便的地方。 然后在初始化browser变量时,将完整路径传递给geckodriver.exe如下所示:

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

错误消息明确指出了该问题: "'geckodriver' executable needs to be in PATH".

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

像这样放置您的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  

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

暂无
暂无

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

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