Selenium Python(3.141.0、Geckodriver 0.30.0)无法打开 Firefox Webdriver:“无法加载您的 Firefox 配置文件,它可能丢失或无法访问。”

Jon*_*des 6 python-3.x selenium-firefoxdriver

已修复:将 Ubuntu 21.04 升级到 21.10 时,firefox(之前使用 apt 安装)被删除并使用 snap 版本安装。逆向(卸载 snap 版本并使用 apt 重新安装)这解决了我的问题。

在 dist 升级后我必须将 Firefox 重置为默认浏览器后,我应该进行调查。


当尝试使用 Selenium for Python 创建 Firefox Webdriver 时,我收到以下消息: “您的 Firefox 配置文件无法加载,它可能丢失或无法访问。” 单击“确定”后,将出现以下堆栈跟踪:

>>> foo = webdriver.Firefox()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
Run Code Online (Sandbox Code Playgroud)

在升级到 Ubuntu 21.10(从 21.04 开始)之前没有发生过这种情况,但我不知道是什么导致了这个错误。尝试使用旧的 Geckodriver (0.29.1) 手动创建驱动程序也不起作用,并会导致相同的错误:

>>> foo = webdriver.Firefox()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
Run Code Online (Sandbox Code Playgroud)

我唯一确信改变的是 python 版本(从 3.9.5 到 3.9.7,但使用带有 3.9.5 的 python-venv 会产生相同的错误)。

我遗漏了一个明显的错误吗?

软件版本/日志:

$ ./geckodriver.bup -V
geckodriver 0.29.1 (970ef713fe58 2021-04-08 23:34 +0200)

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/.
$ python3
Python 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> foo = webdriver.Firefox(executable_path=r'./geckodriver.bup')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
Run Code Online (Sandbox Code Playgroud)
$ cat geckodriver.log 
1635171867200   geckodriver INFO    Listening on 127.0.0.1:37079
1635171867204   mozrunner::runner   INFO    Running command: "/snap/bin/firefox" "--marionette" "-no-remote" "-profile" "/tmp/rust_mozprofileZ5vGbH"
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
amdgpu: os_same_file_description couldn't determine if two DRM fds reference the same file description.
If they do, bad things may happen!
Run Code Online (Sandbox Code Playgroud)
$ geckodriver -V
geckodriver 0.30.0 (d372710b98a6 2021-09-16 10:29 +0300)

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)
$ firefox -V
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
Mozilla Firefox 93.0
Run Code Online (Sandbox Code Playgroud)

(e:尝试使用 selenium==4.0.0 也不起作用)

$ pip freeze | grep selenium
selenium==3.141.0
Run Code Online (Sandbox Code Playgroud)
$ python3 -V
Python 3.9.7
Run Code Online (Sandbox Code Playgroud)

e2:我怀疑错误与创建的临时配置文件的权限有关。但在“/tmp/”中创建的配置文件看起来很干净。它们始终设置为当前用户。手动指定它们,例如

foo = webdriver.Firefox(firefox_profile=r'/tmp/rust_mozprofile7nswon')
Run Code Online (Sandbox Code Playgroud)

返回相同的错误(“无法加载您的 Firefox 配置文件。它可能丢失或无法访问。”)。

Jon*_*des 4

tl;dr: 重新安装 firefox(卸载 snap 包并使用 apt 安装)解决了问题。

我牢记SiKings 的评论,唯一的答案提到“如果您的 Web 浏览器基础版本太旧,请通过 Revo Uninstaller 卸载 Web 浏览器,然后安装最新的 GA 发布版本的 Web 浏览器。” 所以我重新安装了firefox,随后解决了问题。

版本没有变化:

$ firefox -V
Mozilla Firefox 93.0
Run Code Online (Sandbox Code Playgroud)

我认为这很可能是由 dist 升级引起的权限问题。