如何用Python永久修改Firefox的useRecommendedPerformanceSettings设置
永久取消Firefox推荐性能设置的实现方法
核心原理
Firefox的配置会持久保存在用户配置文件的prefs.js中,要让修改永久生效,要么直接修改该配置文件,要么在启动时强制加载自定义偏好,或是使用预先配置好的持久化浏览器配置文件。
方法1:启动时强制指定偏好配置
通过FirefoxOptions直接注入偏好设置,每次用该配置启动浏览器都会自动禁用推荐性能设置,等同于“永久生效”(只要保持代码配置不变)。
代码示例:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.firefox.options import Options options = Options() # 直接设置禁用推荐性能配置的偏好项 options.set_preference("browser.performance.use_recommended_settings", False) driver = webdriver.Firefox(options=options) # 验证设置状态 driver.get('about:preferences') element = driver.find_element(By.ID, "useRecommendedPerformanceSettings") if not element.get_attribute("checked"): print("推荐性能设置已永久禁用(基于启动配置)") driver.quit()
方法2:修改Firefox全局配置文件
直接修改Firefox用户配置文件中的prefs.js,所有使用该配置的启动方式(包括手动打开)都会生效:
- 打开Firefox,输入
about:support - 在「应用程序概要」中找到「配置文件文件夹」,点击「打开文件夹」
- 打开
prefs.js文件,添加或修改以下条目:
user_pref("browser.performance.use_recommended_settings", false);
- 保存文件后重启Firefox即可。
方法3:使用持久化的自定义配置文件
创建一个专属的Firefox配置文件,预先配置好禁用推荐性能设置,之后Selenium每次都调用该配置文件启动:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.firefox_profile import FirefoxProfile # 加载或创建自定义配置文件(替换为你的配置文件路径) profile = FirefoxProfile("/path/to/your/custom/profile") profile.set_preference("browser.performance.use_recommended_settings", False) profile.update_preferences() options = Options() options.profile = profile driver = webdriver.Firefox(options=options) # 验证设置 driver.get('about:preferences') element = driver.find_element(By.ID, "useRecommendedPerformanceSettings") if not element.get_attribute("checked"): print("推荐性能设置已永久禁用(基于持久化配置文件)") driver.quit()
场景说明
- 方法1适合自动化脚本场景,只要代码保留配置,每次启动都会自动应用设置
- 方法2会影响所有使用该配置文件的Firefox启动方式,包括手动打开
- 方法3适合需要隔离测试环境的自动化场景,不会影响本地默认浏览器配置
内容的提问来源于stack exchange,提问作者Sree
相关产品推荐
相关产品推荐

