Selenium控制Firefox时firefox_profile参数弃用警告如何解决
Selenium 4+ 加载Firefox自定义配置文件弃用警告解决方案
问题根因
Selenium 4.0及以上版本已废弃webdriver.Firefox()构造函数的firefox_profile传参方式,所有浏览器配置相关内容需统一通过Options对象传递,因此触发上述弃用警告。
修复方案
方案1:直接传入配置路径(无需修改profile参数时使用)
无需单独实例化FirefoxProfile,直接将配置文件路径赋值给Options对象的profile属性即可:
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.service import Service # 你的Firefox配置文件路径 profile_path = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\y1uqp5mi.default' # 初始化Options对象并绑定配置 options = Options() options.profile = profile_path # 原有service配置保留即可,无特殊配置可省略service参数 # service = Service(executable_path="你的geckodriver本地路径") # 初始化驱动时仅传service和options即可 driver = webdriver.Firefox(service=service, options=options)
方案2:自定义profile参数时使用
如果需要对配置文件做额外的参数设置(比如修改下载路径、禁用弹窗等),可以先实例化FirefoxProfile对象完成配置,再绑定到Options对象:
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.service import Service from selenium.webdriver.firefox.firefox_profile import FirefoxProfile profile_path = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\y1uqp5mi.default' default_profile = FirefoxProfile(profile_path) # 示例:自定义profile配置,设置PDF文件自动下载不弹窗 default_profile.set_preference("browser.download.folderList", 2) default_profile.set_preference("browser.download.dir", r"C:\自定义下载路径") default_profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf") # 绑定到Options对象 options = Options() options.profile = default_profile # 初始化驱动 driver = webdriver.Firefox(service=service, options=options)
内容的提问来源于stack exchange,提问作者cmy2019
相关产品推荐
相关产品推荐

