【问题标题】:DeprecationWarning: firefox_profile has been deprecated, please pass in an Options objectDeprecationWarning: firefox_profile 已被弃用,请传入一个 Options 对象
【发布时间】:2021-12-02 21:42:33
【问题描述】:

首先,我想在 selenium 控制我的 Firefox 时使用一些插件

所以,我尝试在 selenium 代码中加载 firefox 的 默认配置文件

我的代码:

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)

driver = webdriver.Firefox(service=service, options=options, firefox_profile=default_profile)

但是,当我启动代码时,DeprecationWarning 发生了:firefox_profile has been deprecated, please pass in an Options object

我搜索了很多,我认为这不是一个困难的问题,但遗憾的是我最终无法解决这个问题,也许我的英语不好困扰我...... ...

【问题讨论】:

    标签: python selenium firefox deprecation-warning firefox-profile


    【解决方案1】:

    以下是相关文档: https://www.selenium.dev/documentation/webdriver/capabilities/driver_specific_capabilities/#setting-a-custom-profile

    我在本地尝试过,效果很好:

    已编辑:我已更改代码,因此没有弃用警告

    from selenium.webdriver import Firefox
    from selenium.webdriver.firefox.service import Service
    from selenium.webdriver.firefox.options import Options
    
    profile_path = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\y1uqp5mi.default'
    options=Options()
    options.set_preference('profile', profile_path)
    service = Service(r'C:\WebDriver\bin\geckodriver.exe')
    
    driver = Firefox(service=service, options=options)
    
    driver.get("https://selenium.dev")
    
    driver.quit()
    

    【讨论】:

    • 感谢一百万,非常感谢您的及时帮助。 ??
    • 我复制你的代码并测试它,并再次得到 DeprecationWarning:1. DeprecationWarning: firefox_profile 已被弃用,请使用选项对象 default_profile = FirefoxProfile(profile_path) 2. DeprecationWarning: Setting a profile has been已弃用。请使用 set_preference 和 install_addons 方法 options.profile = default_profile 3. DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = Firefox(executable_path='geckodriver.exe', options=options) 我可以解决第三个警告,但其他两个是一样的。
    • 你说得对,我更新了代码,所以我这边没有更多的弃用警告了。
    【解决方案2】:

    此错误消息...

    firefox_profile has been deprecated, please pass in an Options object
    

    ...暗示FirefoxProfile()弃用,而 要使用自定义配置文件,您必须使用Options 的实例。


    DeprecationWarning 与以下 CHANGELOGS 内联:

    • Selenium 4 beta 1

      • 在驱动程序实例化中弃用除 OptionsService 之外的所有参数。 (#9125,#9128)
    • Selenium 4 beta 2

      • 不赞成在选项中使用 Firefox 配置文件
    • Selenium 4 Beta 3

      • 仅在选项中使用 Profile 时才发出弃用警告

    之前通过profile.set_preference()设置的所有配置现在都可以通过options.set_preference()设置如下:

    from selenium.webdriver import Firefox
    from selenium import webdriver
    from selenium.webdriver.firefox.service import Service
    from selenium.webdriver.firefox.options import Options
    
    profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
    options=Options()
    options.set_preference('profile', profile_path)
    options.set_preference('network.proxy.type', 1)
    options.set_preference('network.proxy.socks', '127.0.0.1')
    options.set_preference('network.proxy.socks_port', 9050)
    options.set_preference('network.proxy.socks_remote_dns', False)
    service = Service('C:\\BrowserDrivers\\geckodriver.exe')
    driver = Firefox(service=service, options=options)
    driver.get("https://www.google.com")
    driver.quit()
    

    tl;博士

    Setting a custom profile

    【讨论】:

    • 它对我有用.. 谢谢..
    【解决方案3】:

    我试过了

    from selenium.webdriver.firefox.options import Options
    
    profile_path = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\y1uqp5mi.default'
    options=Options()
    options.set_preference('profile', profile_path)
    driver = Firefox(options=options)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 2017-03-24
      • 2022-06-30
      • 2021-10-29
      • 2017-11-27
      • 2022-11-28
      • 1970-01-01
      相关资源
      最近更新 更多