Selenium Firefox webdriver 导致错误:服务 geckodriver 意外退出。状态码是:2
Posted
技术标签:
【中文标题】Selenium Firefox webdriver 导致错误:服务 geckodriver 意外退出。状态码是:2【英文标题】:Selenium Firefox webdriver results in error: Service geckodriver unexpectedly exited. Status code was: 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
【问题讨论】:
【参考方案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')
你已经完成了......!
【讨论】:
好答案,请记住,当您提供 geckodriver 的可执行路径时,您需要包含整个 exe 地址。看到这个答案:***.com/questions/41919932/…【参考方案2】:我解决了这个问题,我删除了已安装的egg
并重新安装了 selenium,它现在可以正常工作了。
【讨论】:
您能否详细说明egg
的意思,因为我也遇到了同样的问题。
看***.com/questions/2051192/what-is-a-python-egg【参考方案3】:
我在 MacOS Big Sur 上遇到了这个问题,这是一个安全问题。要解决它,请转到您的系统偏好设置 -> 安全和隐私,在底部它会抱怨 geckodriver。您需要点击“仍然允许”。
【讨论】:
以上是关于Selenium Firefox webdriver 导致错误:服务 geckodriver 意外退出。状态码是:2的主要内容,如果未能解决你的问题,请参考以下文章
用于 Firefox 的 Selenium WebDriver [重复]
Selenium w/Firefox 88 更改为 navigator.webdriver
如何隐藏 Firefox 窗口(Selenium WebDriver)?
Selenium Firefox 官方Webdriver -- Geckodriver 下载地址