【问题标题】:Selenium with Firefox webdriver results in error: Service geckodriver unexpectedly exited. Status code was: -11Selenium 与 Firefox webdriver 导致错误:服务 geckodriver 意外退出。状态码是:-11
【发布时间】:2019-05-29 14:14:51
【问题描述】:

我一直在为此绞尽脑汁。我收到了错误:

系统 geckodriver 意外退出。状态码:-11。

我正在使用作为共享托管 Web 服务器的 Linux 服务器。我在虚拟环境中设置了所有内容。

  • Linux 服务器 - CentOS,发行版:7.4.1708
  • Selenium 版本 3.141.0
  • geckodriver 版本 0.23.0
  • 火狐60.3.0
  • Python 3.6.2,不能使用更新的版本

Python、Selenium 和 Geckodriver 位于 Linux Web 服务器上的虚拟环境中。 Firefox 位于虚拟环境之外

export PATH=$PATH:/path/to/geckodriver

到我的终端,让 geckodriver 在PATH 环境变量中使用。

下面是我的代码:

#!/home/DIRECTORY/DIRECTORY/DIRECTORY/venv/bin/python

# -*- coding: UTF-8 -*-
import cgitb
import cgi
from selenium import webdriver
from selenium.webdriver import FirefoxOptions

cgitb.enable()
print ("Content-Type: text/html; charset=utf-8\n\n")
path = r'/home/DIRECTORY/DIRECTORY/DIRECTORY/venv/bin/geckodriver'
binary = FirefoxBinary(r'/usr/lib64/firefox')

opts = FirefoxOptions()
opts.add_argument("--headless")

browser = webdriver.Firefox(firefox_options=opts, firefox_binary=binary, executable_path=path)
rowser.get("http://google.com/")
print ("Headless Firefox Initialized")
browser.quit()

这是我的回溯错误:

Traceback (most recent call last):
File "selen.py", line 20, in <module>
browser = webdriver.Firefox(firefox_options=opts, executable_path=path)
File "/home/DIRECTORY/DIRECTORY/DIRECTORY/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/home/DIRECTORY/DIRECTORY/DIRECTORY/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/home/DIRECTORY/DIRECTORY/DIRECTORY/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /home/DIRECTORY/DIRECTORY/DIRECTORY/venv/bin/geckodriver unexpectedly exited. Status code was: -11

为什么会出现此错误,如何解决?

【问题讨论】:

  • -11 表示子进程中的分段错误,如果有帮助的话。
  • @Jayjayyy:它出现在subprocess documentation 中。 (真 exit status values 是非负数,所以你知道它们在某种程度上很特别。)
  • @Jayjayyy: subprocess 表示由于带有负数的信号(而不是 exit,这是唯一的其他选择)而终止子进程表示信号(并且不能与退出代码混淆)。 (分段错误发生在执行期间,而不是之前,尽管理论上可能发生在forkexec 之前。)
  • 谢谢大家!如何修复 geckodriver.exe 的分段错误?我昨天尝试通过服务器终端运行“geckodriver --version”,但它返回了同样的错误:分段错误。驱动程序的权限设置为 755。
  • 请运行file /home/DIRECTORY/DIRECTORY/DIRECTORY/venv/bin/geckodriver 并发布输出。

标签: python selenium geckodriver


【解决方案1】:

这至少是您问题的部分答案。


过去,某些版本的 Selenium 与某些版本的 Firefox 和/或 geckodriver 无法很好地协同工作。找出您的版本,尽可能将它们更新到最新版本,并查找您的版本的现有错误报告。

以下版本在我的 Ubuntu 18.04 LTS Bionic Beaver 系统上可以很好地协同工作:

  • 检查 Python 版本

    $ python3 --version
    Python 3.6.7
    
  • 检查 Selenium 版本

    $ python3 -c "import selenium; print(selenium.__version__)"
    3.141.0
    
  • 检查火狐版本

    $ firefox --version
    Mozilla Firefox 64.0
    
  • 检查 geckodriver 版本

    $ geckodriver --version
    geckodriver 0.23.0 ( 2018-10-04)
    
    The source code of this program is available from
    testing/geckodriver in https://hg.mozilla.org/mozilla-central.
    
    This program is subject to the terms of the Mozilla Public License 2.0.
    You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
    

如果您必须浏览到特殊路径才能使这些命令起作用,而这些命令不能直接在您的终端或虚拟环境中起作用,您可能需要在对 webdriver.Firefox 的调用中设置以下关键字参数之一:

  • firefox_binary – FirefoxBinary 实例或 Firefox 二进制文件的完整路径。如果未定义,将使用系统默认的 Firefox 安装。
  • executable_path – 覆盖用于 Firefox 47.0.1 及更高版本的 geckodriver 二进制文件的完整路径,默认从系统路径中获取二进制文件。

运行没有什么花哨的测试,但仅在无头模式下运行 Firefox 的 Selenium 的最小示例,例如 minimal_selenium_test.py

import selenium.webdriver

options = selenium.webdriver.FirefoxOptions()
options.add_argument("--headless")

driver = selenium.webdriver.Firefox(firefox_options=options)
driver.get('https://www.python.org/')
print(driver.title)
driver.close()

这应该可以在您的本地笔记本电脑、虚拟服务器以及 Docker 容器内工作,并且应该可以打印:

$ python3 minimal_selenium_test.py 
Welcome to Python.org

【讨论】:

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