Ste*_*enB 5 python selenium geckodriver
我一直在为此绞尽脑汁。我收到错误:
系统 geckodriver 意外退出。状态代码:-11。
我使用的是共享托管 Web 服务器的 Linux 服务器。我在虚拟环境中设置了所有内容。
Python、Selenium 和 Geckodriver 驻留在 Linux Web 服务器上的虚拟环境中。Firefox 驻留在虚拟环境之外
export PATH=$PATH:/path/to/geckodriver
Run Code Online (Sandbox Code Playgroud)
到我的终端以在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()
Run Code Online (Sandbox Code Playgroud)
这是我的回溯错误:
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
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误以及如何修复它?
这至少是对您问题的部分回答。
-11意味着subprocess遇到分段错误。在确定 python 子进程分段错误时阅读有关它的更多信息过去,某些版本的 Selenium 与某些版本的 Firefox 和/或 geckodriver 无法很好地协同工作,因此存在一些问题。找出您的版本,如果可能,将它们更新到最新版本,并查找您的版本的现有错误报告。
以下版本在我的 Ubuntu 18.04 LTS Bionic Beaver 系统上运行良好:
检查 Python 版本
$ python3 --version
Python 3.6.7
Run Code Online (Sandbox Code Playgroud)检查硒版本
$ python3 -c "import selenium; print(selenium.__version__)"
3.141.0
Run Code Online (Sandbox Code Playgroud)检查火狐版本
$ firefox --version
Mozilla Firefox 64.0
Run Code Online (Sandbox Code Playgroud)检查 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/.
Run Code Online (Sandbox Code Playgroud)如果您必须浏览到特殊路径才能使这些命令起作用,而这些命令不能直接在您的终端或虚拟环境中起作用,则您可能需要在对 的调用中设置以下关键字参数之一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()
Run Code Online (Sandbox Code Playgroud)
这应该适用于您的本地笔记本电脑以及虚拟服务器以及 Docker 容器内,并且应该打印:
$ python3 minimal_selenium_test.py
Welcome to Python.org
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2683 次 |
| 最近记录: |