py4u blog

How to Set Up Adblock Plus with EasyList in Selenium Firefox Webdriver Using Python

When automating web browsers with Selenium, ads can be a major annoyance. They slow down page loads, clutter the UI, and may even break test workflows by triggering unexpected pop-ups or dynamic content. To mitigate this, integrating an ad blocker like Adblock Plus with EasyList (the most popular ad-filtering list) into your Selenium Firefox setup is a game-changer.

This guide will walk you through every step to configure Adblock Plus with EasyList in Selenium’s Firefox WebDriver using Python. By the end, you’ll have a browser instance that automatically blocks ads, ensuring faster, cleaner, and more reliable automation.

2026-01

Table of Contents#

  1. Prerequisites
  2. Understanding Adblock Plus and EasyList
  3. Step 1: Download the Adblock Plus XPI File
  4. Step 2: Set Up a Firefox Profile with Adblock Plus
  5. Step 3: Verify Adblock Plus and EasyList Installation
  6. Step 4: Automate with a Python Script
  7. Troubleshooting Common Issues
  8. Conclusion
  9. References

Prerequisites#

Before starting, ensure you have the following installed/configured:

  • Python: Version 3.6 or higher. Download from python.org.
  • Selenium: The Python package for browser automation. Install via pip:
    pip install selenium  
  • Mozilla Firefox: The browser itself. Download from firefox.com.
  • Geckodriver: Firefox’s WebDriver executable. Download the latest version from GitHub and add it to your system’s PATH (or specify its path in your script).
  • A Code Editor: (Optional but recommended) VS Code, PyCharm, or any editor to write/run Python scripts.

Understanding Adblock Plus and EasyList#

  • Adblock Plus: A popular browser extension that blocks ads, pop-ups, and trackers. It works by applying filter lists to identify and block ad content.
  • EasyList: The default and most widely used filter list for Adblock Plus. It contains rules to block ads on thousands of websites, including banners, video ads, and sponsored content. Adblock Plus ships with EasyList pre-enabled, so no extra setup is needed for the list itself—just ensure the extension is installed!

Step 1: Download the Adblock Plus XPI File#

To install Adblock Plus in Selenium, you need the extension’s .xpi (cross-platform install) file. Here’s how to get it:

  1. Visit the Adblock Plus page on Mozilla Add-ons.
  2. Click Add to Firefox → A pop-up will ask for confirmation. Instead of installing directly, right-click the Add button and select Copy Link Address (or similar, depending on your browser).
  3. The copied link will point to the latest Adblock Plus XPI file (e.g., https://addons.mozilla.org/firefox/downloads/file/XXXX/adblock_plus-XXX.xpi).
  4. Download the XPI file using wget (terminal) or a browser:
    wget "PASTE_THE_COPIED_XPI_LINK_HERE" -O adblock_plus.xpi  
    Save it to a directory accessible by your Python script (e.g., ./extensions/adblock_plus.xpi).

Step 2: Set Up a Firefox Profile with Adblock Plus#

Firefox uses profiles to store settings, extensions, and user data. To load Adblock Plus in Selenium, we’ll create a custom profile and add the extension to it.

How to Create a Custom Firefox Profile#

  1. Open Firefox normally and type about:profiles in the address bar.
  2. Click Create a New Profile and follow the prompts to name it (e.g., selenium_adblock_profile). Note the Root Directory path (you’ll need this later).

Alternatively: Let Selenium Create a Temporary Profile#

For automation, Selenium can generate a temporary profile on the fly. This avoids cluttering your system with permanent profiles. We’ll use this approach in the script below.

Step 3: Verify Adblock Plus and EasyList Installation#

Before writing the full script, let’s confirm Adblock Plus loads correctly and uses EasyList.

Key Checks:#

  • Adblock Plus Icon: Should appear in the Firefox toolbar (visible when the browser launches).
  • EasyList Enabled: Go to about:addons → Select Adblock Plus → Options → Under "Filter Lists," ensure "EasyList" is checked.

Step 4: Automate with a Python Script#

Now, let’s write a Python script that initializes Firefox with Adblock Plus and EasyList.

Full Script Example#

from selenium import webdriver  
from selenium.webdriver.firefox.service import Service  
from selenium.webdriver.firefox.options import Options  
import time  
 
# ----------------------  
# Configuration  
# ----------------------  
ADBLOCK_XPI_PATH = "./extensions/adblock_plus.xpi"  # Path to your downloaded XPI  
GECKODRIVER_PATH = "/path/to/geckodriver"  # Update this to your geckodriver path  
TEST_URL = "https://www.cnn.com"  # URL to test ad blocking  
 
# ----------------------  
# Set Up Firefox Profile  
# ----------------------  
# Create a temporary Firefox profile (no need for manual setup)  
firefox_profile = webdriver.FirefoxProfile()  
 
# Add Adblock Plus extension to the profile  
try:  
    firefox_profile.add_extension(extension=ADBLOCK_XPI_PATH)  
    print("Adblock Plus extension added successfully!")  
except Exception as e:  
    print(f"Failed to add Adblock Plus: {e}")  
    exit(1)  
 
# Optional: Disable Firefox notifications/pop-ups (reduces distractions)  
firefox_profile.set_preference("dom.webnotifications.enabled", False)  
firefox_profile.set_preference("dom.push.enabled", False)  
 
# ----------------------  
# Initialize Firefox Driver  
# ----------------------  
# Configure geckodriver service  
service = Service(executable_path=GECKODRIVER_PATH)  
 
# Start Firefox with the custom profile  
driver = webdriver.Firefox(  
    service=service,  
    firefox_profile=firefox_profile  
)  
 
# ----------------------  
# Test Ad Blocking  
# ----------------------  
driver.get(TEST_URL)  
print(f"Loaded: {driver.title}")  
 
# Wait for page to load (adjust based on the site)  
time.sleep(5)  
 
# Optional: Take a screenshot to verify (saves to current directory)  
driver.save_screenshot("adblock_test_screenshot.png")  
print("Screenshot saved as 'adblock_test_screenshot.png'")  
 
# ----------------------  
# Cleanup  
# ----------------------  
# Keep the browser open for manual verification (remove in production)  
input("Press Enter to close the browser...")  
driver.quit()  

Explanation of the Script#

  1. Configuration: Define paths to the Adblock Plus XPI, geckodriver, and a test URL (e.g., CNN, which has ads).
  2. Firefox Profile: Selenium creates a temporary profile, and we add the Adblock Plus XPI to it using add_extension().
  3. Preferences: Disable notifications to avoid interruptions during automation.
  4. Driver Initialization: Start Firefox with the custom profile and geckodriver.
  5. Testing: Load a test URL, wait for ads to load (or be blocked), and save a screenshot for verification.

Step 5: Verify Ad Blocking#

After running the script:

  • Open adblock_test_screenshot.png and check for missing ads (e.g., banner ads, video ads).
  • Manually navigate to about:addons in the Selenium-controlled Firefox window. Select Adblock Plus → Options → Confirm "EasyList" is listed under "Filter Lists" and enabled.

Troubleshooting Common Issues#

1. Geckodriver Not Found#

Error: selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Fix:

  • Add geckodriver to your system’s PATH, or
  • Explicitly specify the path in the Service object (as in the script above).

2. Adblock Plus XPI Not Found#

Error: FileNotFoundError: [Errno 2] No such file or directory: 'adblock_plus.xpi'
Fix:

  • Ensure ADBLOCK_XPI_PATH points to the correct location of the downloaded XPI file.

3. Extension Failed to Load#

Error: selenium.common.exceptions.SessionNotCreatedException: Message: Unable to install extension
Fix:

  • Download the latest Adblock Plus XPI (old versions may be incompatible with newer Firefox releases).
  • Verify the XPI file is not corrupted (re-download if needed).

4. EasyList Not Blocking Ads#

Issue: Ads still appear, even with Adblock Plus installed.
Fix:

  • Check if EasyList is enabled in about:addons → Adblock Plus → Options.
  • Update EasyList: In Adblock Plus options, click Update Now under "Filter Lists."

Conclusion#

By following this guide, you’ve integrated Adblock Plus with EasyList into your Selenium Firefox setup. This ensures faster page loads, cleaner automation workflows, and fewer interruptions from ads.

Whether you’re running UI tests, web scraping, or browser automation, an ad-free environment will make your scripts more reliable and efficient.

References#