WebDriverException:消息:服务 geckodriver 意外退出。状态代码是:在 FreeBSD 监狱中使用 Selenium Geckodriver Firefox 时出现 64 错误

WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: 64 error using Selenium Geckodriver Firefox in FreeBSD jail

为了进行一些测试,我设置了一个全新的 TrueNAS 12.3 FreeBSD Jail 并启动了它,然后安装了 python3firefoxgeckodriverpip使用以下命令:

pkg install python3 firefox geckodriver py38-pip
pip install --upgrade pip
setenv CRYPTOGRAPHY_DONT_BUILD_RUST 1
pip install cryptography==3.4.7
pip install selenium

之后,当我想在我的 Python 代码中将 Selenium 与 Firefox 一起使用时,它不起作用:

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

它带来了

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    self.service.start()
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 110, in assert_process_still_running
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: 64

有趣的是,在我大约一年前设置的另一个 Jail 上(也大致以上述方式设置),它可以正常工作并且不会抛出错误(可能是不同的版本?)!

这是geckodriver.log的唯一内容:

geckodriver: error: Found argument '--websocket-port' which wasn't expected, orisn't valid in this context

USAGE:
    geckodriver [FLAGS] [OPTIONS]

For more information try --help

有什么我可以尝试让它工作的吗?我已经看过 ,但它似乎已经过时了。

Firefox 95.0.2,geckodriver 0.26.0,Python3.8.12,Selenium 4.1.0

这个错误信息...

selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: 64

GeckoDriver 日志...

geckodriver: error: Found argument '--websocket-port' which wasn't expected, orisn't valid in this context

...表示 GeckoDriver was unable to initiate/spawn a new Browsing Context i.e. 会话。


您的主要问题是您使用的二进制文件版本之间不兼容,如下所示:

  • 您的 Selenium Client 版本是 4.1.0.
  • 但是你的 GeckoDriver 版本是 0.26.0.

正如 @ernstki 在他们的 comment 中提到的:

You are running a geckodriver older than 0.30.0, and it is missing the --websocket-port option, which newer/new-ish versions of Selenium seem to depend on.

简单来说,直到v0.29.0的上一个GeckoDriver版本--websocket-port 选项未被使用,现在 Selenium v​​4.0.1.

是强制性的

进一步@whimboo也在他comment中证实:

As it has been manifested the problem here is not geckodriver but Selenium. As such you should create an issue on the Selenium repository instead, so that an option could be added to not always pass the --websocket-port argument. If that request gets denied you will have to use older releases of Selenium if testing with older geckodriver releases is really needed.


解决方案

确保:

  • Selenium 已升级到当前级别 Version 4.1.0
  • GeckoDriver 升级到 GeckoDriver v0.30.0 级别。
  • Firefox 已升级到当前的 Firefox v96.0.2 级别。

FreeBSD 版本

如果您使用的是 GeckoDriver 版本较旧的 FreeBSD 版本,在这些情况下您必须将 Selenium 降级到 v3.x级.

命令():

  • 卸载Selenium:

    pip3 uninstall selenium;
    
  • 安装Selenium:

    pip3 install 'selenium<4.0.0'
    

我将以下代码添加到我的 dockerfile 中,我的问题就解决了。我的问题是版本。

我的问题已通过 Selenium 4.1.0 和 geckodriver v30 解决。

RUN pip install -U selenium==4.1.0
RUN wget https://github.com/mozilla/geckodriver/releases/download/v0.30.0/geckodriver-v0.30.0-linux64.tar.gz
RUN tar -x geckodriver -zf geckodriver-v0.30.0-linux64.tar.gz -O > /usr/local/bin/geckodriver
RUN chmod +x /usr/local/bin/geckodriver
RUN rm geckodriver-v0.30.0-linux64.tar.gz