Ad Blocker with Headless Chrome & Python Selenium
In this article
This guide shows you how to load a real ad blocker extension (like uBlock Origin) into headless Chrome and control it with Python Selenium. This lets you scrape or automate pages without ads cluttering the DOM, speeding up your scripts and making selectors more reliable. You’ll need Python installed, the Selenium package, and a Chrome browser on your machine.
What You'll Need
- Python 3.6+ installed
- Google Chrome browser (the extension must match the Chrome version)
- Selenium package:
pip install selenium - ChromeDriver (or let Selenium Manager handle it automatically in newer versions)
- An ad blocker extension in CRX format (e.g., uBlock Origin from the Chrome Web Store)
Step-by-Step
- Download the extension as a CRX file. Go to the Chrome Web Store page for uBlock Origin, copy the URL, and use a service like
crxextractor.comto download the .crx file. Alternatively, you can get it from the official GitHub releases if available. Save it to a known path, e.g.,ublock.crx. - Install Selenium and verify ChromeDriver. Run
pip install selenium. If you have Selenium 4.6+, ChromeDriver is auto-managed. Otherwise, download the matching ChromeDriver version and place it in your PATH. - Write a Python script that loads the extension. Use
webdriver.ChromeOptions()and add the extension withadd_extension('path/to/ublock.crx'). For headless mode, add--headless=new(or--headlessfor older versions). Example: - Test that the extension loads. Navigate to a page with ads (e.g., a news site) and check the page source for ad-related elements. You can also load
chrome://extensionsin non-headless mode to verify, but in headless you’ll rely on network requests being blocked. - Wait for the extension to initialize. Ad blockers need a moment to load their filter lists. Add a short sleep (e.g., 2–3 seconds) or use Selenium’s
WebDriverWaitto wait for a known element that appears after the extension is active. - Proceed with your automation. Now you can scrape content, click elements, or take screenshots. The ad blocker will intercept requests to known ad domains, so those resources won’t load, making the page lighter and faster.
- Close the driver properly. Always call
driver.quit()to release resources, especially in headless mode where lingering processes can consume memory.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_extension('ublock.crx')
opts.add_argument('--headless=new')
driver = webdriver.Chrome(options=opts)
Common Mistakes
- Using an outdated CRX file. Chrome may reject extensions that don’t match the browser version. Always download a fresh copy.
- Forgetting to wait. The extension may not block ads instantly; if you scrape too soon, you might still see ad elements.
- Using
--headlesswithout the=newflag. Older headless modes have different behavior; use--headless=newfor better compatibility with extensions. - Not enabling extensions in headless mode. Some older Chrome versions disabled extensions by default in headless. The new headless mode supports them, but if you’re on an older version, you may need to use non-headless or a workaround.
Tips
- Use a dedicated profile. Instead of a CRX file, you can load a Chrome profile that already has the extension installed. This avoids CRX compatibility issues.
- Combine with other options. Add
--disable-gpuand--no-sandboxfor stable headless operation on servers. - Check network requests. Use Selenium’s performance log or a proxy like mitmproxy to confirm that ad requests are being blocked.
- Update the extension regularly. Ad filters change frequently; a stale list may let new ads through.
Troubleshooting
- Extension not loading in headless. Try adding
--headless=newand ensure you’re on Chrome 112+. If still failing, switch to a non-headless run to debug, then go back to headless. - ChromeDriver version mismatch. If you get a “session not created” error, download the correct ChromeDriver from chromedriver.chromium.org and specify its path via
webdriver.Chrome(executable_path='...')(or useServicein Selenium 4). - Ads still appear. The extension might not have loaded its filter lists. Wait longer, or manually trigger an update by visiting the extension’s dashboard (not possible in headless). Try a different ad blocker like Adblock Plus.
- Script crashes on exit. Ensure you call
driver.quit()in afinallyblock to avoid zombie Chrome processes.
Frequently Asked Questions
Does the ad blocker work in headless mode?
Yes, with Chrome 112+ and the new headless mode (--headless=new), extensions are fully supported. Older headless modes may not load extensions.
Can I use any ad blocker extension?
Yes, as long as it’s a CRX file that Chrome can install. Common ones include uBlock Origin, Adblock Plus, and AdGuard.
Will this slow down my script?
Initially, the extension may add a few seconds to load. But by blocking ads, page loads become faster, so overall performance usually improves.
How do I get a CRX file for an extension?
You can download it from the Chrome Web Store using third-party tools, or from the developer’s GitHub releases if they provide one. Be cautious with untrusted sources.
Can I load multiple extensions?
Yes, call add_extension() multiple times with different CRX files. However, more extensions may increase startup time and memory usage.
Is this against any terms of service?
Using ad blockers is generally allowed, but some websites may block automated access. Always respect a site’s robots.txt and terms of use.