使用Selenium 4中的option.set_preference在Python中无法加载现有的Firefox配置文件

10

我有这段代码,可以工作并加载火狐浏览器的配置文件

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile


ffOptions = Options()
ffProfile = FirefoxProfile(r'C:\Users\Tyler\AppData\Roaming\Mozilla\Firefox\Profiles\0753x1pz.default')
ffOptions.profile = ffProfile

driver = webdriver.Firefox(options=ffOptions)
driver.get("http://www.google.com")

只有它给出了以下的弃用警告:

firefox_profile已被弃用,请使用Options对象

设置配置文件已被弃用。请使用set_preference和install_addons方法

为了解决这些警告,我尝试更新我的代码为:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options


ffOptions = Options()
ffOptions.set_preference('profile', r'C:\Users\Tyler\AppData\Roaming\Mozilla\Firefox\Profiles\0753x1pz.default')
    
driver = webdriver.Firefox(options=ffOptions)
driver.get("http://www.google.com")

现在没有警告,但当浏览器打开时,配置文件并未设置,它是一个空白配置文件。

1个回答

15

我曾经遇到过同样的问题,这个方法对我有用:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options


ffOptions = Options()

ffOptions.add_argument("-profile")
ffOptions.add_argument(r'C:\Users\Tyler\AppData\Roaming\Mozilla\Firefox\Profiles\0753x1pz.default')
driver = webdriver.Firefox(options=ffOptions)
driver.get("http://www.google.com")

4
这个可以运行,但如果已经有另一个实例打开,Firefox 就无法打开了,你知道有什么解决办法吗? - Tyler
1
我使用一个专门为Selenium创建的配置文件(https://support.mozilla.org/en-US/kb/profile-manager-create-remove-switch-firefox-profiles),这样我就可以在我的个人Firefox配置文件实例运行时打开它。 如果这还不够,也许打开多个选项卡的可能性可以帮助你。 - rentox98
4
这不是一个好的解决方案。Selenium通常会复制配置文件并使用该副本,因此更改不会影响下一次运行。但是您正在指示Firefox直接使用配置文件,这将使任何更改都变为永久性(并影响下一次运行)。 - Niccolo M.
1
那正是我想要的。 - rentox98
1
它完美地运行了,伙计,如果我能的话,我会亲吻你的。非常感谢! - Breno Veríssimo
是的,我正在使用这个 without specifying a profile - 这应该是可以工作的,就像@NiccoloM.建议的那样。但是,我仍然收到了弃用警告。 - leanne

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