Selenium升级4.0.0b4报Unrecognised option moz:debuggerAddress如何解决
问题原因
Selenium 4.0.0b4版本对Firefox驱动的默认配置做了调整,启动时会自动添加moz:debuggerAddress参数,如果你本地使用的GeckoDriver版本低于v0.30.0,就会因为不识别该参数抛出报错。
解决方案
方案1:升级GeckoDriver(推荐)
直接将GeckoDriver升级到v0.30.0及以上版本,升级后原有代码无需任何修改即可正常运行,是适配Selenium 4.x版本的最优方案。
方案2:手动移除默认添加的参数
如果暂时无法升级GeckoDriver,可以在初始化配置时主动删除moz:debuggerAddress参数,修改后的代码如下:
import selenium from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() options.headless = True # 清除自动生成的moz:debuggerAddress配置 if "moz:debuggerAddress" in options.capabilities: del options.capabilities["moz:debuggerAddress"] driver = webdriver.Firefox(options=options) driver.get("https://google.com.au")
注意事项
需保证Firefox浏览器版本和GeckoDriver版本匹配,避免出现其他兼容性问题。
内容的提问来源于stack exchange,提问作者John Hon
相关产品推荐
相关产品推荐

