【发布时间】: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