Selenium with Firefox webdriver results in error: Service geckodriver unexpectedly exited. Status code was: -11

Selenium with Firefox webdriver results in error: Service geckodriver unexpectedly exited. Status code was: -11

为此我绞尽脑汁。我收到错误:

System geckodriver unexpectedly exited. Status code: -11.

我正在使用 Linux 服务器,它是一个共享托管 Web 服务器。我在虚拟环境中设置了所有内容。

Python、Selenium 和 Geckodriver 驻留在 Linux 网络服务器上的虚拟环境中。 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 表示 subprocess 遇到分段错误。在 Determining if a python subprocess segmentation faults
  • 阅读更多相关信息

过去,某些版本的 Selenium 与某些版本的 Firefox and/or 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 版本

    $ 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/.
    

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

  • firefox_binary – Instance of FirefoxBinary or full path to the Firefox binary. If undefined, the system default Firefox installation will be used.
  • executable_path – Full path to override which geckodriver binary to use for Firefox 47.0.1 and greater, which defaults to picking up the binary from the system path.

运行 没有什么特别的测试,只是 Selenium 和 Firefox 在无头模式下的最小示例,例如 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