DeprecationWarning:firefox_profile已被弃用,请传入一个Options对象

cmy*_*019 25 python firefox selenium deprecation-warning firefox-profile

首先,我想在 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)
Run Code Online (Sandbox Code Playgroud)

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

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

小智 28

以下是相关文档: https : //www.selenium.dev/documentation/webdriver/capability/driver_specific_capability/#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()
Run Code Online (Sandbox Code Playgroud)

  • 我已复制并粘贴您的代码,仅更改路径,没有弃用警告,但配置文件也没有加载,此代码对我不起作用。更多详细信息:/sf/ask/5003229031/ 因为我不确定这是否只是我一个人。 (6认同)

Deb*_*anB 17

这个错误信息...

firefox_profile has been deprecated, please pass in an Options object
Run Code Online (Sandbox Code Playgroud)

...意味着FirefoxProfile()弃用,并且使用要使用自定义配置文件,您必须使用Options.


弃用警告与以下变更日志一致:

  • 硒 4 β 1

    • 弃用驱动程序实例化中的所有 butOptionsService参数。(#9125,#9128)
  • 硒 4 β 2

    • 弃用在选项中使用 Firefox 配置文件
  • 硒 4 β 3

    • 仅当选项中使用配置文件时才给出弃用警告

之前设置的所有配置现在都可以通过如下方式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()
Run Code Online (Sandbox Code Playgroud)

TL; 博士

设置自定义配置文件