Selenium Firefox webdriver导致错误:服务geckodriver意外退出。状态代码为:2

时间:2016-09-04 01:57:29

标签: python selenium firefox exception selenium-webdriver

我正在编写一个程序来搜索网站上的文章中的特定条目,我使用selenium webdriver for Python。

尝试连接到网站时,我遇到了这个例外:

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连接

3 个答案:

答案 0 :(得分:6)

在最新的Firefox浏览器中运行python selenium测试(上面的第47版)

“Marionette”或“Gecko Driver”是firefox驱动程序的未来版本。 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

#提供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')

你完成了......!

答案 1 :(得分:1)

我修复此问题,删除了已安装并重新安装selenium的egg,现在效果很好。

答案 2 :(得分:0)

我在MacOS Big Sur上遇到了这个问题,这是一个安全问题。要解决此问题,请转到系统偏好设置->安全和隐私,并在底部显示geckodriver。您需要单击“仍然允许”。

相关问题