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

Selenium运行报错:不支持在普通用户会话中以root身份运行Firefox

解决Selenium Firefox测试进程意外关闭的问题

嘿,从你的geckodriver日志里已经明明白白找到问题根源啦——你正在以root用户身份在普通用户的会话中运行Firefox,这是Firefox明确禁止的行为,日志里的这句就是关键提示:

Running Firefox as root in a regular user's session is not supported. ($HOME is /home/test which is owned by test.)

这就是导致selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1错误的直接原因,Firefox拒绝在这种权限不匹配的环境下启动。

结合你的AWS环境(非管理员),给你几个可行的解决方案:

1. 切换到普通用户运行脚本(最推荐)

既然$HOME属于test用户,那直接切换到test用户来执行你的Selenium脚本就好:

  • 如果是在终端操作,执行命令切换用户:
    su - test
    
    然后再运行你的Python测试脚本。
  • 如果是通过自动化任务或服务启动的脚本,联系管理员帮忙把执行上下文改成test用户,避免用root权限运行。

2. 强制指定root可访问的Firefox配置目录(如果必须用root运行)

如果你因为环境限制不得不以root身份运行,可以手动指定一个root有读写权限的临时目录作为Firefox的配置文件目录:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
# 指定root可写的临时目录作为profile路径
options.add_argument('-profile')
options.add_argument('/tmp/root_firefox_profile')
driver = webdriver.Firefox(options=options)

或者用FirefoxProfile对象来指定:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

# 创建并指定root可访问的profile目录
profile = FirefoxProfile('/tmp/root_firefox_profile')
driver = webdriver.Firefox(firefox_profile=profile)

3. 解决Chrome启动挂起的问题

你提到切换到Chrome也有问题,大概率也是权限导致的——Chrome默认也禁止root用户运行,需要添加--no-sandbox参数,同时AWS环境下建议加上--disable-dev-shm-usage避免内存不足的问题:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)

另外,因为你提到环境在AWS且不是管理员,建议先找管理员确认下最近是否有用户权限、环境变量或者系统配置的变更,这可能是突然出现问题的原因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:39:04