我一直为此而绞尽脑汁。我遇到了错误:
系统壁虎驱动程序意外退出。状态码:-11。
我使用的是共享主机Web服务器的Linux服务器。我已经在虚拟环境中设置了一切。
Python,Selenium和Geckodriver驻留在Linux Web服务器上的虚拟环境中。 Firefox驻留在虚拟环境之外
export PATH=$PATH:/path/to/geckodriver
到我的终端以在PATH
环境变量中使用geckodriver。
下面是我的代码:
#!/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
为什么会出现此错误以及如何解决?
答案 0 :(得分:2)
这至少是您问题的部分答案。
-11
表示subprocess
遇到分段错误。在Determining if a python subprocess segmentation faults 过去,某些版本的Selenium不能与某些版本的Firefox和/或geckodriver配合使用时出现一些问题。找出您的版本,并尽可能将其更新为最新版本,并查找您的版本的现有错误报告。
以下版本在我的Ubuntu 18.04 LTS Bionic Beaver系统上可以很好地协同工作:
检查Python版本
$ python3 --version
Python 3.6.7
检查硒的版本
$ 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/.
如果您必须浏览到特殊路径以使这些命令有效,而这些命令在您的终端或虚拟环境中不直接起作用,则可能需要在对{{3 }}:
firefox_binary
– Firefox二进制实例或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