我正在编写一个程序,用于在网站上搜索文章中的特定条目,我正在使用适用于 Python 的 selenium webdriver。
在尝试连接到该站点时出现此异常:
Traceback (most
recent call last):
File "search.py", line 26, in <module>
test.search_for_keywords()
File "search.py", line 13, in search_for_keywords
browser = webdriver.Firefox()
File "C:\Python27\lib\site-packages\selenium-3.0.0b2-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 65, in __init__
self.service.start()
File "C:\Python27\lib\site-packages\selenium-3.0.0b2-py2.7.egg\selenium\webdriver\common\service.py", line 86, in start
self.assert_process_still_running()
File "C:\Python27\lib\site-packages\selenium-3.0.0b2-py2.7.egg\selenium\webdriver\common\service.py", line 99, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: 2
它说网络驱动程序意外退出。我该如何解决这个问题?我正在尝试使用 python 版本 2.7.12 连接 firefox 版本 48.0
最佳答案
在最新的 Firefox 浏览器(版本 47 以上)中运行 python selenium 测试
“Marionette”或“Gecko Driver”是 firefox 驱动程序的 future 版本。 Firefox 47+ 与 Selenium 2.53 中使用的驱动程序不兼容,Selenium 3+ 将使用名为“Marionette”或“Gecko Driver”的新驱动程序(尚未正式发布)。
先决条件:
• Mozilla firefox : 版本 50.0.2(版本 47 以上)
• Selenium:版本 3.0.2
• Geckodriver:版本 0.11.1
• Python:版本 2.7.3
设置:
• Selenium :pip install –U selenium
• Geckodriver:从 https://github.com/mozilla/geckodriver/releases 下载 geckodriver , 解压文件并将其放在文件夹中
• 使用 geckodriver 路径设置“Path”环境变量
示例脚本:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#Provide Firefox 二进制路径
binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe’)
caps = DesiredCapabilities.FIREFOX.copy()
#Set ‘marionette’ 浏览器为 True
caps['marionette'] = True
#通过指定 geckodriver 可执行路径启动 Firefox 实例
driver = webdriver.Firefox(firefox_binary=binary,capabilities=caps, executable_path`='D:/Installers/geckodriver-v0.11.1-win64/geckodriver')
你完成了......!
关于python - Selenium Firefox webdriver 导致错误 : Service geckodriver unexpectedly exited. 状态代码为:2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39312903/