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

24

首先,我想要在Selenium控制Firefox时使用一些插件。

因此,我尝试在Selenium代码中加载Firefox的默认配置文件。

我的代码:

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

我的代码:

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已经被弃用,请传入一个Options对象

我搜索了很多资料,我不认为这是一个难题,但可悲的是最终我无法解决这个问题,也许是我的英语不好阻碍了我... ...

3个回答

27

这是它的文档: 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()

非常感谢您的帮助,我真的非常感激您及时的支持。 - cmy2019
我复制了你的代码并测试了一下,又得到了DeprecationWarning警告:
  1. DeprecationWarning: firefox_profile已被弃用,请使用Options对象 default_profile = FirefoxProfile(profile_path)
  2. DeprecationWarning: 设置配置文件已被弃用,请使用set_preference和install_addons方法 options.profile = default_profile
  3. DeprecationWarning: executable_path已被弃用,请传入一个Service对象 driver = Firefox(executable_path='geckodriver.exe', options=options) 我可以解决第三个警告,但其他两个警告是相同的。
- cmy2019
你说得对,我更新了代码,所以我的端上不再有弃用警告。 - Atanas Atanasov
6
我已经复制并粘贴了您的代码,只更改了路径,没有弃用警告,但配置文件也没有加载,这段代码对我没有起作用。 更多细节请参见:https://dev59.com/vMPra4cB1Zd3GeqPrun1 因为我不确定是否只是我个人的问题。 - Tyler

17

这个错误信息...

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

    • 在 Options 中使用 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()

简介

设置自定义配置文件


1
由于此错误,这个功能无法按预期工作。我发现通过使用问题中提供的解决方案即options.add_argument("-profile")options.add_argument(profile_path),可以使其正常工作。 - Saifur Rahman Mohsin
“仅在选项中使用Profile时才发出弃用警告”- 我没有使用Profile,但仍然收到此警告。 - leanne
我的代码:driver = webdriver.Firefox(driver_path, options=options, service=service) - 我的options和service值都没有包含任何有关firefox配置文件的内容。 - leanne

0

我试过这个

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接