【问题标题】:Error update preferences in Firefox profile: 'Options' object has no attribute 'update_preferences'Firefox 配置文件中的错误更新首选项:“选项”对象没有属性“更新首选项”
【发布时间】:2022-01-02 01:02:11
【问题描述】:

我无法更新我的 Firefox 配置文件首选项。如果我添加 options.update_preferences () 我得到一个错误。我得到 AttributeError: 'Options' object has no attribute 'update_preferences'

我该如何解决?

PS:我写了这段代码,它可能对 Stack Overflow 社区有用,因为已经使用多年的 Firefox 与首选项的连接现在已被弃用,因为 firefox_profile 已被 Options 对象和 executable_path 替换通过服务对象

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 = '/home/xxxx/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default'

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)

options.update_preferences() #here

service = Service('/usr/bin/geckodriver')
driver = Firefox(service=service, options=options)
  
driver.get("https://www.google.com")
driver.quit()

【问题讨论】:

  • 似乎不需要。将其排除在代码之外有问题吗?
  • @pcalkins options.update_preferences 用于使 options.set_preference 生效。如果没有首选项更新,则 options.set_preference 不适用
  • 我自己从来没有使用过它。 (无论如何在 Selenium 3 中......虽然我在 Java 中做了一个 options.setProfile(profile) 方法......)你在这里使用 Selenium 4 吗?
  • @pcalkins 我不明白你的回答,抱歉。但是我使用的是 4.1.0 版。如何更新和应用这些选项?

标签: python python-3.x selenium selenium-webdriver firefox


【解决方案1】:

update_preferences() 通过 keyvalue 对更新 FirefoxProfile.DEFAULT_PREFERENCES。它在FirefoxProfile() 类中,现在已弃用

相反,您必须使用Options,您的有效工作代码块将是:

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 = '/home/xxxx/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default'

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('/usr/bin/geckodriver')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
driver.quit()

PS:请注意,当您使用options.set_preference() 时,您不再需要update_preferences()


参考文献

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

  • 我收到一个错误。我得到错误驱动程序 = Firefox (service = service, firefox_profile = profile) NameError: name 'Firefox' is not defined。我还注意到您已从 selenium.webdriver.firefox.options 中删除了导入选项。我添加它是因为使用经典的 Firefox 代码和每个人都使用的 Tor 代理,并且已经在网络上运行了多年,我收到错误说要使用新的服务和选项模块,因为 FirefoxProfile 已被弃用。消息是“firefox_profile 已被弃用,请传入一个 Options 对象”。
  • 后来添加了from selenium.webdriver import Firefox :)
  • 我仍然得到一个错误,我之前说的那个:驱动程序 = Firefox (service = service, firefox_profile = profile) DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object.出于这个原因,我试图编写新代码,即问题中的代码。它工作得很好。问题只是更新。你还有其他建议吗?谢谢:)
  • 查看更新后的答案。现在完美运行。
  • @DebanjanB我不再需要 update_preferences 了吗?因为?所以如果我不再需要它,我的代码也很好。有什么区别?谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
相关资源
最近更新 更多