Ubuntu Server上使用Selenium(无头Firefox)启动报错255求助
解决Ubuntu Server上Selenium无头Firefox启动失败(status 255)的问题
我之前在Ubuntu Server环境下部署无头Firefox+Selenium时,也碰到过一模一样的Process unexpectedly closed with status 255错误,给你几个经过验证的排查和解决方向:
1. 补全Firefox无头模式所需的系统依赖
Ubuntu Server默认没有图形环境相关的依赖库,即使是无头模式,Firefox也需要一些基础库才能正常运行。执行以下命令安装缺失的依赖:
sudo apt-get update && sudo apt-get install -y libgtk-3-0 libdbus-glib-1-2 libxt6 libx11-xcb1 libxcomposite1 libxdamage1 libxfixes3 libxi6 libxrandr2 libxrender1 libasound2 libpangocairo-1.0-0 libcups2 libatk1.0-0 libatk-bridge2.0-0
2. 确保geckodriver与Firefox版本兼容
你日志里显示用的是geckodriver 0.20.0,这个版本仅支持Firefox 57~60版本。如果你的Firefox版本过高或过低,都会导致启动失败:
- 查看当前Firefox版本:
firefox --version - 如果版本不匹配,要么升级/降级geckodriver,要么调整Firefox版本。比如Firefox 70+需要geckodriver 0.26.0及以上版本。
3. 改用Firefox原生无头模式,弃用pyvirtualdisplay
从Firefox 56开始就支持原生无头模式了,不需要再依赖pyvirtualdisplay,反而额外的虚拟显示层可能引发冲突。修改你的代码如下:
from selenium import webdriver from selenium.webdriver.firefox.options import Options # 配置Firefox无头模式参数 firefox_options = Options() firefox_options.add_argument("--headless") firefox_options.add_argument("--disable-gpu") firefox_options.add_argument("--no-sandbox") # Ubuntu Server下root用户运行时必须加这个参数 # 初始化浏览器 browser = webdriver.Firefox(options=firefox_options)
4. 解决权限问题
如果是用root用户运行脚本,Firefox可能会因为安全限制无法启动。除了上面代码里的--no-sandbox参数,你也可以创建一个普通用户,切换到该用户下运行脚本:
sudo adduser selenium-user sudo su - selenium-user # 然后在该用户环境下安装依赖和运行脚本
5. 查看完整的geckodriver日志定位问题
你提供的日志片段不够完整,建议指定日志路径获取详细错误信息:
browser = webdriver.Firefox(options=firefox_options, service_log_path='/tmp/geckodriver_full.log')
运行后查看/tmp/geckodriver_full.log,日志里通常会明确提示错误原因(比如缺失某个库、权限问题等)。
内容的提问来源于stack exchange,提问作者user9115052
相关产品推荐
相关产品推荐

