Selenium,如何用插件启动Firefox?

jgi*_*ich 5 python firefox selenium

我想加载Firefox Addon 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()
Run Code Online (Sandbox Code Playgroud)

没有错误,根据文档它应该工作,但它没有,它仍然开始没有插件.

我试过的下一件事就是这样称呼它:

self.driver = webdriver.Firefox(browser_profile=firefoxProfile)
Run Code Online (Sandbox Code Playgroud)

输出:

TypeError: __init__() got an unexpected keyword argument 'browser_profile'
Run Code Online (Sandbox Code Playgroud)

但这是python的一个方面,我不太了解.我有这个想法,因为看起来那样.

小智 7

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

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

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

driver.install_addon('/Users/someuser/app/extension.xpi', temporary=True)
Run Code Online (Sandbox Code Playgroud)

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


amp*_*ata 6

我没有足够的#1 代表留下你的问题中留言,可惜我不知道回答你的问题,但对于它的价值,你需要调用webdriver.Firefox()firefox_profile,不browser_profile,因为你做了.

另请参阅:http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/firefox/webdriver.py#33


小智 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://.....
Run Code Online (Sandbox Code Playgroud)