Python Using Adblock with Selenium and Firefox Webdriver

davelupt picture davelupt · Dec 30, 2013 · Viewed 14.1k times · Source

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

Answer

Wladimir Palant picture Wladimir Palant · Dec 30, 2013

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");