Firefox 配置文件中的错误更新首选项:'Options' 对象没有属性 'update_preferences'

Error update preferences in Firefox profile: 'Options' object has no attribute 'update_preferences'

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

我该如何解决?

P.S:我写了这段代码,它可能对 Stack Overflow 社区有用,因为使用了多年的 Firefox 连接与偏好设置现在已被弃用,因为 firefox_profile 已替换为选项对象,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()

update_preferences() updates the FirefoxProfile.DEFAULT_PREFERENCES through key, value pairs. It in the FirefoxProfile() class 现在 已弃用 .

相反,您必须使用 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: Note that when you use options.set_preference() you no more require update_preferences()


参考资料

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

  • DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object