Python Using Adblock with Selenium and Firefox Webdriver

ⅰ亾dé卋堺 提交于 2019-11-29 07:45:32

问题


My goal is to use Adblock Plus with Selenium via Python. I have been able to get it to the point where it will load the extension, but by default it does not include the default filter "EasyList." Here is what I have so far:

 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)

Most of this code was ripped off of http://selenium-python.readthedocs.org/en/latest/faq.html


回答1:


Actually, Adblock Plus will add EasyList by default - but not if you set extensions.adblockplus.currentVersion preference to disable update/first-run actions. I guess that your goal was preventing the first-run page from showing up but it also prevented data storage initialization. Note that you have more issues here: even if Adblock Plus adds EasyList, it will still take an unknown time to download.

The better course of action should be initializing your profile with an existing adblockplus/patterns.ini file. You can get this file from your regular Firefox profile, with EasyList and other filter settings, and copy it to /Users/username/Downloads/profilemodel/adblockplus/patterns.ini. Then the following should work:

ffprofile = webdriver.FirefoxProfile("/Users/username/Downloads/profilemodel");



回答2:


There's a better way to do this:

1) extract adblock.xpi with 7-zip or equivalent

2) open /modules/AppIntegration.jsm with a regular text editor

3) find the function declaration for "notifyUser()", and replace this with a simple return. for example:

/**
* 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;
}

Now you just need to package the files back into a zip, and change the extension from .zip to .xpi -- Voila!

This will stop adblock from loading the welcome page, but it will still configure the necessary subscription settings. Make sure NOT to call

ffprofile.set_preference("extensions.adblockplus.currentVersion", "x.x.x")

Otherwise, it will not know to "boot itself"

Please note that this is for adblock_plus-2.0.3 since I'm using firefox-17. The code may be slightly different, and in a different location for newer versions. See: https://issues.adblockplus.org/ticket/206#comment:5



来源:https://stackoverflow.com/questions/20832159/python-using-adblock-with-selenium-and-firefox-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!