【问题标题】:WebDriverException: Message: invalid argument: can't kill an exited process with GeckoDriver, Selenium and Python on RaspberryPi3WebDriverException:消息:无效参数:无法在 RaspberryPi3 上使用 GeckoDriver、Selenium 和 Python 终止已退出的进程
【发布时间】:2019-03-03 05:32:44
【问题描述】:

服务器:树莓派 3
操作系统:Dietpi - 版本 159
Geckodriver 版本:0.22 for arm
火狐版本:52.9.0
Python 版本:3.5
硒版本: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?:\n')
password = input('What is your password?:\n')
...
...

输出:

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

知道有什么问题吗?我试过谷歌但没有运气。

【问题讨论】:

    标签: python selenium firefox raspberry-pi3 geckodriver


    【解决方案1】:

    正如 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()
    

    【讨论】:

    • 这是我为解决问题而采取的确切故障排除步骤。 Error: cannot open display: :0.0 是我在日志中看到的消息。我的 Python 进程中的环境变量 DISPLAY 试图使用不存在的显示 :0.0。将该环境变量更改为正确的显示是解决方法。
    【解决方案2】:

    拇指规则

    浏览器在启动期间崩溃的一个常见原因是在 Linux 上以 root 用户 (administrator) 运行 WebDriver 启动的浏览器。虽然可以通过在创建 WebDriver 会话时传递 --no-sandbox 标志来解决此问题,但不支持并且强烈建议不要使用此类配置。您需要将环境配置为以普通用户身份运行浏览器。


    此错误消息...

    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.0Firefox 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 and Firefox Browser compatibility chart

    【讨论】:

    • 以root身份运行,我认为不会有问题。是的,谢谢。当我设置自定义功能时,尽管根本原因相同,但错误却有所不同。粘贴在这里供任何人搜索:Unable to find a matching set of capabilities (Selenium::WebDriver::Error::SessionNotCreatedError)
    • 我将所有内容更新到最新版本,geckodriver 位于我计算机上的正确位置(运行 ubuntu 19.04),但我仍然收到与原始问题相同的错误消息。不知道现在该怎么办
    • @DebanjanB 在机器人框架中安装了 python 3.6.9、Firefox 77.0.1 和 geckodriver 0.26.0 在 ubuntu 18.04 中出现 WebDriverException: Message: invalid argument: can't kill an exited process 错误你能帮忙吗?
    • 我在独立的 selenium 服务器下运行 Firefox 时遇到了这个错误。重新启动 selenium 服务器似乎可以解决(MacOS)
    • 更改进程所有者为我工作:我从 Visual Studio Code 内部移动到 OS 内置终端。虽然我无法在我的 Linux 系统中识别进程所有者,但我使用 WebDriver 方法调用 driver = webdriver.Firefox(firefox_binary='/path/to/firefox') 解决了这个问题。我从 VSC 内部遇到了多个异常(取决于我尝试了哪些技巧,从终端 $ python mytest.py 或在编辑器的调试模式下运行脚本。
    【解决方案3】:

    由于此错误可能有许多不同的潜在原因,因此最好找到设置 selenium 以使用调试级别日志记录的根本原因。就我而言,对于带有水豚的 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.
    

    安装后一切正常。

    【讨论】:

      【解决方案4】:

      这个解决方案对我有用

      from selenium import webdriver
      from selenium.webdriver.firefox.options import Options
      
      options = Options()
      options.headless = True
      driver = webdriver.Firefox(options=options)
      

      【讨论】:

      • 如果您收到 WebDriverException 错误,并且您拥有最新版本的 firefox 和 geckdriver 并且您使用的是无头计算机,则需要按照 Anar 在这里提到的那样设置 headless = True。
      【解决方案5】:

      我遇到了同样的问题,并意识到真正的问题是我正在测试的 docker 容器中没有安装一些 Firefox 依赖项。

      尝试发起firefox并检查是否返回错误。

      【讨论】:

        【解决方案6】:

        在我的例子中,我以 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
        

        我并没有说它是正确的,但它完成了我的工作

        【讨论】:

          【解决方案7】:

          如果您在没有显示的系统上运行 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

          【讨论】:

          • 谢谢,在运行 ubuntu 16.04 的服务器上遇到类似问题时,这对我有用
          • 它也对我有用。需要注意的是,firefox 本身必须安装在系统中。至少对我来说,上面的这段代码只有在安装了 firefox 之后才能工作。
          • 我正在使用 docker selenium 网格,图像为:selenium/node-firefox-debug:3.141.59。它表明它具有兼容的版本。但我仍然面临上述错误。
          【解决方案8】:

          我处于无头模式,使用正确版本的所有内容,摆脱此错误消息的唯一方法是以 root 身份执行 selenium 测试

          【讨论】:

          • 只是为了给人们更多信息以防万一:我am以root身份运行一个进程并且它正在工作。我的问题是我需要使用无头选项运行。
          【解决方案9】:

          我用过:

          • VS 代码
          • Linux/Ubuntu:18.10
          • Nightwatch.js

          我的问题是我试图 VS Code 终端运行 Nightwatch(它会自动启动 GeckoDriver)。

          【讨论】:

          • 从终端(在 VSCode 之外)运行它也为我解决了这个问题,运行 Firefox 68.0.1.、Selenium 3.141.0 和 geckodriver v0.24.0。该设置is compatible 然后在从常规终端调用它后实际工作(即使没有无头)。
          【解决方案10】:

          是选中在构建可以解决问题之前启动 Xvfb,但是如果您有像管道或多分支管道这样的工作,则此选项不可见。在您执行所需测试的 Selenium 网格节点中:

          1- 安装 Xvfb:apt install xvfb

          2- 执行 Xvfb:/usr/bin/Xvfb :99 -ac -screen 0 1024x768x8 &amp; export DISPLAY=":99"

          3- 重新运行您的节点,例如:java -jar selenium.jar -role node -hub http://#.#.#.#:4444/grid/register -capabilities browserName=firefox,plataform=linux -host #.#.#.# -port 1991

          【讨论】:

          • 由于在无头模式下运行浏览器,因此不需要 xvfb。
          • 如何重启(实际杀死)并启动 /usr/bin/Xvfb ?
          • 这应该是最佳答案 - Firefox 不会在 X 会话之外运行,所以如果有人通过 docker 或 ssh 运行,请确保启动 Xvfb 以模拟它
          【解决方案11】:

          我能够通过使用 Xvfb 运行测试来解决此问题。我在远程服务器上运行它们。

          我使用的是 Jenkins,所以我选中了如下所示的框:

          感谢https://www.obeythetestinggoat.com/book/chapter_CI.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-12-26
            • 2020-09-10
            相关资源
            最近更新 更多