【问题标题】:Selenium Firefox webdriver results in error: Service geckodriver unexpectedly exited. Status code was: 2Selenium Firefox webdriver 导致错误:服务 geckodriver 意外退出。状态码是:2
【发布时间】:2017-01-11 18:43:33
【问题描述】:

我正在编写一个程序,它将在网站上搜索文章中的特定条目,我正在使用 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

表示 webdriver 意外退出。我该如何解决这个问题?我正在尝试使用 python 版本 2.7.12 连接 firefox 版本 48.0

【问题讨论】:

    标签: python selenium firefox exception selenium-webdriver


    【解决方案1】:

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

    “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 路径设置“路径”环境变量

    示例脚本:

    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()
    

    #将'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')
    

    你已经完成了......!

    【讨论】:

    【解决方案2】:

    我解决了这个问题,我删除了已安装的egg 并重新安装了 selenium,它现在可以正常工作了。

    【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2019-05-29
      • 2017-04-21
      • 2022-01-24
      • 2017-08-31
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多