如何禁用Selenium启动时geckodriver版本检查或屏蔽报错?
解决无网络环境下Selenium调用Firefox的版本检查报错问题
以下是两种正规的解决方法,无需用try-catch包裹代码即可避免该报错:
方法一:禁用geckodriver版本检查
直接通过FirefoxOptions添加参数阻止geckodriver发起版本更新请求:
from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() # 添加禁用geckodriver更新检查的参数 options.add_argument("--disable-geckodriver-update") driver = webdriver.Firefox(options=options) # 执行你的自动化操作 driver.quit()
方法二:关闭Firefox自身的更新与兼容性检查
通过配置Firefox偏好设置,彻底关闭应用自动更新和扩展兼容性检查,进一步避免网络请求:
from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() # 禁用Firefox自动更新 options.set_preference("app.update.enabled", False) options.set_preference("app.update.auto", False) # 禁用扩展兼容性检查 options.set_preference("extensions.checkCompatibility", False) options.set_preference("extensions.checkUpdateSecurity", False) # 同时禁用geckodriver版本检查 options.add_argument("--disable-geckodriver-update") driver = webdriver.Firefox(options=options) # 执行你的自动化操作 driver.quit()
这两种方法都是官方支持的配置方式,能从根源上阻止无网络环境下的版本检查请求,避免出现DNS相关报错。
内容的提问来源于stack exchange,提问作者ordinary_python_programmer
相关产品推荐
相关产品推荐

