You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu22.04上Selenium/Django/Firefox运行报错:权限及Marionette端口异常

问题:AWS EC2 Ubuntu 22.04下Firefox+Selenium运行Django项目的权限与Marionette超时问题

环境信息

  • 系统:AWS EC2 Ubuntu 22.04
  • Firefox:110.0.1(非Snap方式安装)
  • Selenium:4.1
  • GeckoDriver:0.32.2
  • 运行载体:Django项目

获取驱动的代码

options = webdriver.FirefoxOptions()
options.headless = True
if not driver:
   driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), options=options)

GeckoDriver错误日志

1678369806282   geckodriver     INFO    Listening on 127.0.0.1:57825
1678369806786   mozrunner::runner       INFO    Running command: "/bin/firefox" "--marionette" "-headless" "--remote-debugging-port" "50887" "--remote-allow-hosts" "localhost" "-no-remote" "-profile" "/tmp/rust_mozprofileq2trdx"
*** You are running in headless mode.
[GFX1-]: glxtest: Unable to open a connection to the X server
[GFX1-]: RenderCompositorSWGL failed mapping default framebuffer, no dt

(firefox:18279): dconf-CRITICAL **: 13:51:06.828: unable to create file '/var/www/.cache/dconf/user': Permission denied.  dconf will not work properly.

堆栈跟踪错误

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.10/dist-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/opt/ondefend/Blindspot/./apps/universal/decorators.py", line 57, in wrap
    return function(request, *args, **kwargs)
  File "/opt/ondefend/Blindspot/./apps/react/views/reporting_views.py", line 678, in download_report
    report.make_charts_react(f'http://'+request.META['HTTP_HOST'])
  File "/opt/ondefend/Blindspot/./apps/reporting/models.py", line 667, in make_charts_react
    screenshot.screenshot_charts_react(self.project, f'media/reports/{self.customer.name}/{self.name}/')
  File "/opt/ondefend/Blindspot/./apps/reporting/screenshot.py", line 75, in screenshot_charts_react
    driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), options=options)
  File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/firefox/webdriver.py", line 179, in __init__
    RemoteWebDriver.__init__(
  File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 268, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 359, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Failed to read marionette port

解决方案

1. 解决X服务器与图形渲染问题

EC2服务器无图形环境,headless模式仍需虚拟X支持,安装xvfb:

sudo apt-get update && sudo apt-get install -y xvfb

同时给Firefox添加必要的headless参数,避免渲染错误:

options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")

如果需要,也可以用xvfb-run包裹Django启动命令,确保虚拟X环境生效:

xvfb-run -a python manage.py runserver

2. 修复dconf权限问题

不要直接设置777权限,改为将目录所属者设为运行Django的用户(通常是www-data):

sudo mkdir -p /var/www/.cache/dconf
sudo chown -R www-data:www-data /var/www/.cache/dconf
sudo chmod 700 /var/www/.cache/dconf

3. 调整Marionette连接配置

Selenium 4.1推荐使用Service类管理驱动,显式指定端口并增加超时时间:

from selenium.webdriver.firefox.service import Service

options = webdriver.FirefoxOptions()
options.headless = True
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
options.add_argument("--marionette-port=2828")  # 显式指定端口

service = Service(executable_path=GeckoDriverManager().install())
service.start_timeout = 30  # 延长启动超时

if not driver:
    driver = webdriver.Firefox(service=service, options=options)

4. 确认版本兼容性

GeckoDriver 0.32.2与Firefox 110.0.1版本匹配(0.32.x适配Firefox 109-111),无需调整版本。

内容的提问来源于stack exchange,提问作者Michael McDermott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 16:40:13