我的目标是通过Python将Adblock Plus与Selenium一起使用。我已经能够将它加载到加载扩展名的位置,但默认情况下它不包括默认过滤器“EasyList”。这是我到目前为止:
from selenium import webdriver
from time import sleep
ffprofile = webdriver.FirefoxProfile()
adblockfile = '/Users/username/Downloads/adblock_plus-2.4-tb+fx+an+sm.xpi'
ffprofile.add_extension(adblockfile)
ffprofile.set_preference("extensions.adblockplus.currentVersion", "2.4")
browser = webdriver.Firefox(ffprofile)
while(True):
browser.get("www.cnn.com")
sleep(5)
大部分代码都被扯掉了 http://selenium-python.readthedocs.org/en/latest/faq.html
实际上,Adblock Plus默认会添加EasyList - 但如果设置则不会 extensions.adblockplus.currentVersion
首选项禁用更新/首次运行操作。我想你的目标是阻止第一次运行的页面显示,但它也阻止了数据存储初始化。请注意,此处有更多问题:即使Adblock Plus添加了EasyList,下载仍然需要一段时间。
更好的做法应该是使用现有的初始化您的个人资料 adblockplus/patterns.ini
文件。您可以使用EasyList和其他过滤器设置从常规Firefox配置文件中获取此文件,然后将其复制到 /Users/username/Downloads/profilemodel/adblockplus/patterns.ini
。然后以下应该工作:
ffprofile = webdriver.FirefoxProfile("/Users/username/Downloads/profilemodel");
有一个更好的方法来做到这一点:
1)使用7-zip或同等数据提取adblock.xpi
2)使用常规文本编辑器打开/modules/AppIntegration.jsm
3)找到“notifyUser()”的函数声明,并用简单的返回替换它。例如:
/**
* function notifyUser()
* {
* let wrapper = (wrappers.length ? wrappers[0] : null);
* if (wrapper && wrapper.addTab)
* {
* wrapper.addTab("chrome://adblockplus/content/ui/firstRun.xul");
* }
* else
* {
* Utils.windowWatcher.openWindow(wrapper ? wrapper.window : null,
* "chrome://adblockplus/content/ui/firstRun.xul",
* "_blank", "chrome,centerscreen,resizable,dialog=no", null);
* }
* }
*/
function notifyUser()
{
return;
}
现在你只需要将文件打包回拉链,然后将扩展名从.zip更改为.xpi - 瞧!
这将阻止adblock加载欢迎页面,但仍会配置必要的订阅设置。确保 不 打电话
ffprofile.set_preference("extensions.adblockplus.currentVersion", "x.x.x")
否则,它将无法“启动自己”
请注意,这是针对adblock_plus-2.0.3,因为我使用的是firefox-17。代码可能略有不同,并且在较新版本的不同位置。看到: https://issues.adblockplus.org/ticket/206#comment:5