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

YoY*_*ome 4 python firefox selenium 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
Run Code Online (Sandbox Code Playgroud)

它说网络驱动程序意外退出.我该如何解决这个问题?我正在尝试使用python版本2.7.12与firefox版本48.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

建立:

•硒: 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
Run Code Online (Sandbox Code Playgroud)

#提供Firefox二进制路径

binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe’)

caps = DesiredCapabilities.FIREFOX.copy()
Run Code Online (Sandbox Code Playgroud)

#将'marionette'浏览器设置为True

caps['marionette'] = True
Run Code Online (Sandbox Code Playgroud)

#通过指定geckodriver可执行路径来启动Firefox实例

driver = webdriver.Firefox(firefox_binary=binary,capabilities=caps, executable_path`='D:/Installers/geckodriver-v0.11.1-win64/geckodriver')
Run Code Online (Sandbox Code Playgroud)

你做完了......!