使用Python+Selenium自动化Firefox时遭遇验证码及“浏览器正在被自动化”检测的解决方法咨询
使用Python+Selenium自动化Firefox时遭遇验证码及“浏览器正在被自动化”检测的解决方法咨询
Good day,
我正在用 Python 结合 Selenium 和 Firefox 做浏览器自动化测试,但过程中总是遇到验证码阻碍脚本运行,还收到提示“浏览器正在被自动化”(原因:Marionette)。
我的自动化流程如下:
- 打开 Firefox
- 访问 www.google.com
- 搜索“YouTube”
- 点击 YouTube 链接
- 搜索目标视频
- 播放视频
但流程中频繁出现验证码,我怀疑是 Firefox 检测到了自动化操作导致的。
在 Chrome 里,我通过以下配置成功降低了自动化检测概率:
options = webdriver.ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("useAutomationExtension", False) user_data_dir = os.path.join(os.getcwd(), "chrome_user_data") options.add_argument(f"user-data-dir={user_data_dir}") service = webdriver.chrome.service.Service(ChromeDriverManager().install()) driver = webdriver.Chrome(service=service, options=options)
为了在 Firefox 里实现类似效果,我已经尝试了以下配置,但似乎都不起作用:
options = webdriver.FirefoxOptions() options.set_preference("dom.webdriver.enabled", False) # 禁用WebDriver标识 options.set_preference("dom.webnotifications.enabled", False) # 禁用通知 options.set_preference("media.peerconnection.enabled", False) # 禁用WebRTC(可能暴露机器人) options.set_preference("network.http.referer.spoofSource", True) # 伪造来源引用 options.set_preference("privacy.trackingprotection.enabled", False) # 避免触发机器人标识 options.set_preference("useAutomationExtension", False) options.set_preference("general.platform.override", "Win32") # 模拟Windows系统 options.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") # 伪造User-Agent # 启动WebDriver service = Service(GeckoDriverManager().install()) driver = webdriver.Firefox(service=service, options=options) driver.get("https://www.google.com")
我还尝试了以下操作:
- 使用本地 Firefox 配置文件:
profile_path = "/path/to/firefox/profile" options.add_argument(f"-profile {profile_path}") - 执行脚本修改
navigator.webdriver属性:driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")driver.execute_script("window.navigator.webdriver = false;")
尽管做了这些尝试,我还是会遇到验证码和自动化检测提示。
请问有没有有效的方法可以在 Firefox 中禁用自动化检测,或者配置 Marionette 来避免验证码出现?任何指导或变通方案我都非常感激。
谢谢!
备注:内容来源于stack exchange,提问作者elctl02
相关产品推荐
相关产品推荐

