WebDriverException:消息:无效参数:无法使用 RaspberryPi3 上的 GeckoDriver、Selenium 和 Python 终止已退出的进程
- 2025-01-16 08:37:00
- admin 原创
- 80
问题描述:
服务器:Raspberry Pi 3
操作系统:Dietpi - 版本 159
Geckodriver 版本:0.22 for arm
Firefox 版本:52.9.0
Python 版本:3.5
Selenium 版本:3.14.1
Gecko 是可执行文件,位于 /usr/local/bin/
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
import time
options = Options()
options.set_headless(headless=True)
driver = webdriver.Firefox(firefox_options=options)
print('Need your login credential')
username = input('What is your username?:
')
password = input('What is your password?:
')
...
...
输出:
root@RPi3:~# python3.5 ITE-bot.py
Traceback (most recent call last):
File "ITE-bot.py", line 12, in <module>
driver = webdriver.Firefox(firefox_options=options)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process
知道哪里出了问题吗?我试过谷歌搜索,但没有成功。
解决方案 1:
如果您在没有显示屏的系统上运行 Firefox,请确保使用无头模式。
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
另外,请确保您拥有兼容版本的 Firefox、Selenium 和 Geckodriver:
https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html
解决方案 2:
经验法则
root
浏览器在启动过程中崩溃的一个常见原因是在 Linux 上以用户 ( )身份运行 WebDriver 启动的浏览器administrator
。虽然可以--no-sandbox
在创建 WebDriver 会话时传递标志来解决此问题,但这种配置不受支持且极不鼓励。您需要将环境配置为以普通用户身份运行浏览器。
此错误信息...
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process
...意味着GeckoDriver无法启动/产生新的WebBrowsing 会话,即Firefox 浏览器会话。
您的主要问题是您使用的二进制文件版本不兼容,如下所示:
您的GeckoDriver版本是0.22.0。
GeckoDriver v0.21.0(2018-06-15)的发行说明明确提到以下内容:
Firefox 57(及更高版本)
Selenium 3.11(及更高版本)
您的Firefox版本是52.9.0。
因此, GeckoDriver v0.22.0与Firefox Browser v57之间存在明显不匹配
解决方案
将GeckoDriver升级到GeckoDriver v0.22.0级别。
GeckoDriver 位于指定位置。
GeckoDriver 对非root用户具有可执行权限。
升级Firefox版本至Firefox v62.0.2级别。
通过IDE清理项目工作区,然后仅使用所需的依赖项重建项目。
如果您的基本Web Client版本太旧,请通过Revo Uninstaller将其卸载并安装最新的 GA 和发布版本的Web Client。
以非 root 用户身份执行Selenium 测试。
GeckoDriver、Selenium和Firefox 浏览器兼容性图表
解决方案 3:
我处于无头模式,使用所有内容的正确版本,摆脱此错误消息的唯一方法是不以 root 身份执行 selenium 测试
解决方案 4:
是的,选中“在构建之前启动 Xvfb”可以解决问题,但如果您有像管道或多分支管道这样的作业,则此选项不可见。在您执行测试的 Selenium 网格节点中,您需要:
1-安装Xvfb:apt install xvfb
2-执行Xvfb:/usr/bin/Xvfb :99 -ac -screen 0 1024x768x8 & export DISPLAY=":99"
3-重新运行你的节点,例如:java -jar selenium.jar -role node -hub http://#.#.#.#:4444/grid/register -capabilities browserName=firefox,plataform=linux -host #.#.#.# -port 1991
解决方案 5:
这个解决方案对我有用
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
解决方案 6:
由于此错误可能有许多不同的潜在原因,因此最好找到根本原因,将 selenium 设置为使用调试级别日志记录。 在我的例子中,对于带有 capybara 的 Ruby,我需要设置:Selenium::WebDriver.logger.level = :debug
。 然后,运行相同的规范,我可以在日志中看到缺少依赖项,在我的例子中:
libdbus-glib-1.so.2: cannot open shared object file: No such file or directory
Couldn't load XPCOM.
安装后一切运行正常。
解决方案 7:
我用过:
VS 代码
Linux/Ubuntu:18.10
Nightwatch.js
我的问题是我尝试从VS Code 终端运行 Nightwatch(它会自动启动 GeckoDriver)。
解决方案 8:
我遇到了同样的问题,并意识到真正的问题是我正在测试的docker容器内没有安装一些firefox依赖项。
尝试启动firefox
并检查它是否返回错误。
解决方案 9:
正如 Nico 和 jay 所说,您需要检查日志以查看错误的详细信息。由于您可能使用不同的系统,因此您可以指定存储日志的路径(即“/tmp/geckodriver.log”)。
from selenium import webdriver
firefox_options = webdriver.firefox.webdriver.Options()
driver = webdriver.Firefox(log_path="/tmp/geckodriver.log",
options=firefox_options)
就我的情况而言,日志内容如下:
Error: no DISPLAY environment variable specified
通过在启动驱动程序之前在选项中添加无头模式解决了这个问题。使用以下行:
firefox_options.set_headless()
解决方案 10:
我能够通过使用 Xvfb 运行测试来解决这个问题。我在远程服务器上运行它们。
我使用的是 Jenkins,因此我选中了如下复选框:
来源:https://www.obeythetestinggoat.com/book/chapter_CI.html
解决方案 11:
就我而言,我以 root 身份运行测试用例
geckodriver.log
1576076416677 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilenCbl2e"
Running Firefox as root in a regular user's session is not supported. ($HOME is /home/seluser which is owned by seluser.)
1576077143004 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile7wpSQ7"
1576077143689 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons
1576077143689 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: telemetry
1576077143689 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/
1576077143689 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: about:reader*
1576077145372 Marionette INFO Listening on port 35571
1576077145423 Marionette WARN TLS certificate errors will be ignored for this session
1576077200207 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilenhoHlr"
Running Firefox as root in a regular user's session is not supported. ($HOME is /home/seluser which is owned by seluser.)
我可以绕过
cd /home
chown -R root seluser
我不能说它正确但它完成了我的工作