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

Python 3 中 Selenium 4 为 Firefox 浏览器添加扩展的正确方法

Selenium 4 Python 环境为Firefox添加扩展的规范实现

问题根因

  • Selenium 4 中add_extension方法仅归属于FirefoxProfile类,未迁移到Options类,直接调用options.add_extension会触发属性不存在错误
  • 直接给webdriver.Firefox()构造函数传递profile参数已被弃用,需要将配置好的profile绑定到Options对象后传入

正确实现代码

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

# 初始化火狐配置对象,需要复用本地已有配置则传入profile_path,不需要可省略参数
profile = webdriver.FirefoxProfile(profile_path)
# 加载xpi格式扩展文件
profile.add_extension('adblock_plus-3.10.2-an+fx.xpi')

# 初始化Options对象,绑定配置好的profile
options = Options()
options.profile = profile

# 启动驱动
service = Service('/usr/local/bin/geckodriver')
driver = webdriver.Firefox(options=options, service=service)

补充说明

该写法适配所有Selenium 4版本,无弃用警告,可正常加载扩展。如果需要安装多个扩展,重复调用profile.add_extension()传入对应xpi文件路径即可。

内容的提问来源于stack exchange,提问作者P A N

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 01:06:08