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

Flask+Celery环境下无头Selenium报WebDriverException状态127错误

Flask+Celery环境下无头Selenium运行报错Status 127的解决方法

问题场景

在Flask应用中通过Celery任务运行无头模式Selenium时,出现以下错误:

selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 127

提供的最简可复现代码如下:

app.py

from flask import Flask, request
from celery import Celery
import os

app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = os.getenv('CELERY_BROKER_URL')
app.config['CELERY_RESULT_BACKEND'] = os.getenv('CELERY_RESULT_BACKEND')

celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

@celery.task
def run_selenium_task(referral_id, comment):
    # Insert your Selenium script here

@app.route('/run_selenium', methods=['POST'])
def run_selenium():
    referral_id = request.form['referral_id']
    comment = request.form['comment']
    run_selenium_task.delay(referral_id, comment)
    return 'Selenium task started'

if __name__ == '__main__':
    app.run()

Selenium脚本

from selenium.webdriver.firefox.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("-headless")
firefox_binary_path = '/usr/bin/firefox'
geckodriver_path = '/home/fusion/bin/geckodriver'
options.binary_location = firefox_binary_path
service = Service(executable_path=geckodriver_path)
driver = webdriver.Firefox(service=service, options=options)

# Insert the rest of your Selenium code here

错误原因及解决步骤

1. 检查可执行文件的存在性与权限

状态码127核心原因是系统找不到指定的可执行文件,或者运行用户无执行权限:

  • 验证文件路径是否正确:
    ls -l /usr/bin/firefox /home/fusion/bin/geckodriver
    
  • 给GeckoDriver添加执行权限:
    chmod +x /home/fusion/bin/geckodriver
    

2. 确保Firefox与GeckoDriver版本兼容

Selenium对Firefox和GeckoDriver的版本匹配要求严格,版本不兼容会直接导致进程崩溃:

  • 查看当前Firefox版本:
    firefox --version
    
  • 下载对应版本的GeckoDriver(例如Firefox 115+需搭配GeckoDriver 0.33+),替换现有文件后重新测试。

3. 补充无头运行的依赖(服务器环境)

如果在无图形界面的Linux服务器上运行,需要安装Firefox无头模式所需的依赖库:

# Ubuntu/Debian系统执行
apt-get install -y firefox-esr xvfb libx11-xcb1 libdbus-glib-1-2

也可以通过pyvirtualdisplay虚拟显示环境:

# 在Selenium代码中添加以下内容
from pyvirtualdisplay import Display

# 启动虚拟显示
display = Display(visible=0, size=(1920, 1080))
display.start()

# 初始化driver(原有代码)
options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("-headless=new")  # 新版Firefox推荐使用-headless=new
firefox_binary_path = '/usr/bin/firefox'
geckodriver_path = '/home/fusion/bin/geckodriver'
options.binary_location = firefox_binary_path
service = Service(executable_path=geckodriver_path)
driver = webdriver.Firefox(service=service, options=options)

# 执行完任务后关闭显示
driver.quit()
display.stop()

使用前需安装依赖包:pip install pyvirtualdisplay

4. 调整Celery运行环境的PATH变量

如果GeckoDriver不在系统默认PATH中,Celery进程可能无法找到它:

  • 启动Celery时追加PATH:
    export PATH=$PATH:/home/fusion/bin && celery -A app.celery worker --loglevel=info
    
  • 或者在Python代码中手动添加路径:
    import sys
    sys.path.append('/home/fusion/bin')
    

5. 开启详细日志排查深层问题

在Celery任务中添加日志输出,获取更底层的错误信息:

import logging

@celery.task
def run_selenium_task(referral_id, comment):
    logging.basicConfig(level=logging.DEBUG)
    try:
        options = Options()
        options.add_argument("--window-size=1920,1080")
        options.add_argument("-headless=new")
        firefox_binary_path = '/usr/bin/firefox'
        geckodriver_path = '/home/fusion/bin/geckodriver'
        options.binary_location = firefox_binary_path
        # 生成GeckoDriver日志文件
        service = Service(executable_path=geckodriver_path, log_path='./geckodriver.log')
        driver = webdriver.Firefox(service=service, options=options)
        
        # 执行你的业务逻辑
        # ...
        
        driver.quit()
    except Exception as e:
        logging.error(f"任务执行失败: {str(e)}")
        raise

查看生成的geckodriver.log文件,能定位到更具体的启动失败原因。


内容的提问来源于stack exchange,提问作者Mike C.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 07:27:52