Selenium,如何启动带有插件的Firefox?

10

我想加载Firefox插件RequestPolicy。这是我的尝试方法:

rp = open(wd + "/requestpolicy.xpi")
firefoxProfile = FirefoxProfile()
firefoxProfile.add_extension(rp)

self.driver = webdriver.Firefox(firefoxProfile)

self.usr = user.User(self.driver, username, password, world)
self.usr.login()

根据文档,没有错误,应该可以工作,但实际上它并没有生效,仍然在没有插件的情况下启动。接下来我尝试了另一种调用方式:
self.driver = webdriver.Firefox(browser_profile=firefoxProfile)

输出:

TypeError: __init__() got an unexpected keyword argument 'browser_profile'

但这是我不太熟悉的 Python 方面。我得到这个想法是因为源代码看起来是这样的。

4个回答

7

6

我花了几个小时才找到解决方案。

你需要做的就是将扩展名下载为.xip文件。

然后将以下代码行添加到您的代码中:

driver.install_addon('/Users/someuser/app/extension.xpi', temporary=True)

“/Users/someuser/app/extension.xpi” 替换为您的扩展.xip文件的路径。


这个方法在C# API中不存在 - Profile.AddExtension也不起作用... - Andre
是的,因为我们正在谈论Python :) - Arthur Stepanov

4
我所做的并且有效的事情是:
profile=webdriver.FirefoxProfile()
profile.add_extension("/home/.../.mozilla/firefox/zrdb9ki8.default/extensions/{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}.xpi") # for adblockplus

profile.set_preference("extensions.adblockplus.currentVersion", "2.8.2")
Fox = webdriver.Firefox(profile)
Fox.get(website_Url) #https://.....

1
此外,您不应直接打开xpi文件。相反,请尝试只提供地址:
firefoxProfile.add_extension(wd + "/requestpolicy.xpi")

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