Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Using Adblock with Selenium and Firefox Webdriver

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

like image 670
davelupt Avatar asked Dec 30 '13 01:12

davelupt


2 Answers

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");
like image 133
Wladimir Palant Avatar answered Nov 08 '22 11:11

Wladimir Palant


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

like image 2
user2426679 Avatar answered Nov 08 '22 13:11

user2426679