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

AWS EC2实例中Firefox搭配Seleniumwire运行d.requests返回空列表求助

AWS EC2 ARM环境下Selenium Wire搭配Firefox时d.requests返回空列表问题

环境配置

  • ubuntu==20.4
  • selenium-wire==4.3.0
  • python==3.8.10
  • geckodriver==0.29.1
  • ChromeDriver==94.0.4606.81
  • 64-bit (Arm)

依赖安装方式

通过sudo apt-get install chromium-chromedriver安装ChromeDriver,通过sudo apt-get install firefox-geckodriver安装geckodriver。

测试脚本

from seleniumwire import webdriver

options = webdriver.FirefoxOptions()
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--ignore-certificate-errors-spki-list')
options.add_argument('--ignore-ssl-errors')
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-gpu")

d = webdriver.Firefox(options=options)
d.get("https://google.com")
print(d.requests)

问题描述

d.requests返回[]不符合预期,print(d.page_source)运行正常,仅Firefox环境下复现,Chrome下相同代码可正常返回非空请求列表,因项目要求无法切换到Chrome。初步判断为代理配置错误,公开解决方案均未生效。

已尝试的调试步骤

  • 显式设置端口,配置selenium_options={"port": 4444},4444为命令行运行geckodriver时localhost返回的端口
  • 添加自定义代理配置:
options = {
        'proxy': {
            'http': 'http://myusername:password@myproxyserver.com:123456', 
            'https': 'http://myusername:password@myproxyserver.com:123456',
            'no_proxy': 'localhost,127.0.0.1'
        }  
    }
  • 全新EC2实例重新安装所有依赖后运行脚本
  • 指定不同tmp目录修复权限问题

调试建议

Selenium Wire对Firefox的代理注入逻辑和Chrome不同,Firefox需要单独配置证书和代理规则才能捕获请求,可按以下顺序排查:

  1. 显式禁用Firefox的系统代理和DNS over HTTPS
    Firefox默认开启的DNS over HTTPS会直接绕过Selenium Wire的本地代理,是该场景下最常见的问题,在FirefoxOptions中额外添加以下配置:
options.set_preference('network.proxy.allow_hijacking_localhost', True)
options.set_preference('network.trr.mode', 0) # 禁用DNS over HTTPS,避免请求绕过代理
options.set_preference('network.proxy.type', 1) # 手动指定代理,不使用系统配置
  1. 正确传入Selenium Wire配置
    你之前的自定义代理配置没有显式传入seleniumwire_options参数,会导致配置不生效,修改初始化逻辑:
wire_options = {
    'verify_ssl': False,
    'ssl_insecure': True,
    # 你自定义的代理配置也放在这里
    'proxy': {
        'http': 'http://myusername:password@myproxyserver.com:123456', 
        'https': 'http://myusername:password@myproxyserver.com:123456',
        'no_proxy': 'localhost,127.0.0.1'
    }  
}
d = webdriver.Firefox(options=options, seleniumwire_options=wire_options)
  1. 添加等待逻辑再读取请求列表
    driver.get()返回后部分异步请求可能还未完成,添加显式等待后再读取请求:
import time
d.get("https://google.com")
time.sleep(5) # 也可以用WebDriverWait等待特定元素加载完成
print(len(d.requests))
  1. 检查版本兼容性
    Ubuntu 20.04 apt源安装的Firefox版本可能过高,和geckodriver 0.29.1不兼容,可降级Firefox到91.x版本,该版本和geckodriver 0.29.1、Selenium Wire 4.3.0的兼容性经过验证。
  2. 关闭Firefox的沙箱安全策略
    Arm环境下Firefox的沙箱可能会拦截代理配置,在启动参数中额外添加options.add_argument('--disable-dev-shm-usage')。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 18:45:02