如何使用Python版Selenium以正常模式启动Firefox浏览器?
解决Selenium启动Firefox进入测试模式的问题
Selenium默认会创建临时Firefox配置文件并启用测试相关参数,导致浏览器显示“测试模式”。以下两种方法可以启动正常模式:
方法一:使用Firefox默认用户配置文件
直接调用你日常使用的Firefox配置文件,会加载书签、插件等个人设置:
找到你的Firefox配置文件路径:
- Windows:
%APPDATA%\Mozilla\Firefox\Profiles\xxxxx.default-release - macOS:
~/Library/Application Support/Firefox/Profiles/xxxx.default-release - Linux:
~/.mozilla/firefox/xxxx.default-release
- Windows:
修改代码指定配置文件:
from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() # 替换为你的实际配置文件路径 options.add_argument("-profile") options.add_argument("C:/Users/你的用户名/AppData/Roaming/Mozilla/Firefox/Profiles/xxxxxx.default-release") driver = webdriver.Firefox(options=options) driver.get("http://github.com")
方法二:禁用测试模式标识
使用临时配置但关闭自动化测试相关的提示和标识,适合不需要个人配置的场景:
from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() # 禁用WebDriver检测 options.set_preference("dom.webdriver.enabled", False) options.set_preference("useAutomationExtension", False) # 关闭测试模式横幅 options.add_argument("--disable-blink-features=AutomationControlled") driver = webdriver.Firefox(options=options) driver.get("http://github.com")
内容的提问来源于stack exchange,提问作者Adem Dişbudak
相关产品推荐
相关产品推荐

