【问题标题】:Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11无法打开与“会话”消息总线的连接:无法在没有 $DISPLAY 的 X11 的情况下自动启动 dbus-daemon
【发布时间】:2026-02-15 07:30:02
【问题描述】:

我可以在 linux 上运行脚本,但在同一台机器上,如果 apache 运行脚本,我会得到 TimeoutException. Message: connection refused

查看 geckodriver.log,它说:

1604976847149   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilehZbDYR" 

Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

Running without a11y support!

*** You are running in headless mode.

我认为这部分只是一个警告 - 不是错误?我不确定。

要启动 Firefox,我基本上是这样做的:https://github.com/timgrossmann/InstaPy/blob/master/instapy/browser.py

不使用 XVFB 或任何虚拟显示器,仅使用“-headless”和 Firefox 扩展。

知道为什么这段代码可以在命令行上完美运行,但如果从 apache 调用就不行?

这是完整的跟踪:

TimeoutException: Message: connection refused

  File "django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "mainapp/views.py", line 277, in api
    send_message(message, recipient_username)
  File "scripts/browser_login.py", line 434, in send_message
    driver = browser_login(username, password)
  File "scripts/browser_login.py", line 123, in browser_login
    options=firefox_options,
  File "selenium/webdriver/firefox/webdriver.py", line 162, in __init__
    keep_alive=True)
  File "selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)

这恰好在脚本启动 60 秒后发生。它位于 webdriver 初始化。

【问题讨论】:

    标签: selenium firefox geckodriver


    【解决方案1】:

    知道为什么这段代码可以在命令行上完美运行,但如果从 apache 调用它就不行?
    这恰好在启动脚本后 60 秒发生。它位于 webdriver 初始化。

    可能是geckodriver安装失败或者geckodriver与Firefox版本不兼容,为了防止这种情况使用geckodriver-autoinstaller自动下载安装支持当前安装版本Firefox的geckodriver

    1. 将以下模块添加到requirements.txt 并安装。
    geckodriver-autoinstaller==0.1.0
    
    1. browser.py 中删除您之前的@​​987654330@ 安装代码,并在开头添加以下行。
    import geckodriver_autoinstaller  
    geckodriver_autoinstaller.install()  
    
    1. 如果您正在执行远程执行,请在每次执行任何 python 文件之前进行必要的设置以运行命令 pip install -r requirements.txt
    2. 最后运行脚本browser.py

    【讨论】:

    • 在我的测试环境中,Firefox 是 68.12.0esr,geckodriver 是 0.26.0。
    • 根据Mozilla geckodriver supported platforms,对于 Firefox 版本 >= 60,geckodriver 版本应该 >= 0.26.0 和 Selenium 版本 >= 3.11 和 Python 3.14。检查是否所有这些都满足。
    • Selenium 是 3.11 但 Python 是 2.7
    • 您可以升级到 Python 3 或在 line 38 指定 geckodriver 的版本,否则它将获取不支持 Python 2.7 的最新版本。指定版本为gdd.download_and_install("v0.20.1"),同时使用 Selenium 3.5 和 55 到 62 之间的 Firefox 版本。