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

使用Python+Selenium时modify_header扩展及请求头加载失败但浏览器正常启动问题(含最新版Selenium报错)

Fix for Modify Header Extension & Request Headers Not Loading in Selenium Firefox

Hey there, let's get this sorted out! The warnings in your console are actually pointing straight to the root of the problem: firefox_profile has been deprecated in newer Selenium versions (4.x+), and using it is preventing your extension and header configurations from loading properly. We need to switch to using the Options object instead, which is the recommended way to configure Firefox now.

Here's the corrected code with explanations:

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

# Initialize Firefox Options instead of FirefoxProfile
firefox_options = Options()

# Add the Modify Header extension (use raw string for Windows path to avoid escape issues)
firefox_options.add_extension(extension=r"C:\Users\User\PycharmProjects\Resource Files\modify_header_headers-0.1.7.xpi")

# Set the preferences for your custom headers
firefox_options.set_preference("modifyheaders.headers.count", 2)
# First header: x-msisdn
firefox_options.set_preference("modifyheaders.headers.action0", "Add")
firefox_options.set_preference("modifyheaders.headers.name0", "x-msisdn")
firefox_options.set_preference("modifyheaders.headers.value0", '00012345731')
firefox_options.set_preference("modifyheaders.headers.enabled0", True)
# Second header: Custom User-Agent
firefox_options.set_preference("modifyheaders.headers.action1", "Add")
firefox_options.set_preference("modifyheaders.headers.name1", "User-Agent")
firefox_options.set_preference("modifyheaders.headers.value1", 'Opera/9.80 (Android; Opera Mini/12.0.1987/37.7327; U; pl) Presto/2.12.423 Version/12.16')
firefox_options.set_preference("modifyheaders.headers.enabled1", True)

# Initialize the driver with Options instead of firefox_profile
se = webdriver.firefox.service.Service(GeckoDriverManager().install())
driver = webdriver.Firefox(service=se, options=firefox_options)

driver.get("url")
driver.maximize_window()
# Check if headers are applied
driver.get("url/omt/ha")

Key changes made:

  • Replaced FirefoxProfile with Options (the modern, supported configuration method for Selenium 4+)
  • Used firefox_options.add_extension() instead of fp.add_extension()
  • Moved all preference settings to the Options object using set_preference()
  • Used a raw string (r"path") for the extension file path to avoid Windows backslash escape errors
  • Cleaned up unused imports (the Edge Service import wasn't needed for Firefox)

Additional checks to ensure everything works:

  1. Verify extension compatibility: Make sure the modify_header_headers-0.1.7.xpi extension is compatible with your installed Firefox version. Older extensions might fail to load in newer Firefox builds.
  2. Check path correctness: Double-check that the extension file path is accurate—typos here will prevent the extension from being added.
  3. Test headers directly: You can use browser dev tools (Network tab) to inspect requests and confirm your custom headers are being sent.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 05:32:44