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

10 投票
4 回答
17913 浏览
提问于 2025-04-17 02:38

我想加载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 个回答

5

我做了以下事情,并且成功了:

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://.....
7

我花了几个小时才找到解决办法。

你只需要把你的扩展下载成 .xip 文件。

然后在你的代码里加上这一行:

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

"/Users/someuser/app/extension.xpi" 替换成你 .xip 文件的路径。

7

我在Stackoverflow上的声望不够,无法对你的问题留言,而且我也不知道你的问题的答案。不过,想告诉你的是,你需要用firefox_profile来调用webdriver.Firefox(),而不是像你现在这样用browser_profile

另外,你可以看看这个链接:http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/firefox/webdriver.py#33

撰写回答