User Agents

Web browsers and everyday web tech, explained

Block Ads in Automated Browser Sessions

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Automated browser sessions—such as those driven by Selenium, Puppeteer, or Playwright—often load pages full of ads, which slow down scripts, consume bandwidth, and interfere with element selectors. Blocking ads in these sessions makes tests and scrapers faster, more reliable, and less prone to flaky failures. This guide covers the most effective methods, from browser flags to network-level filtering, and explains how to implement them in your automation stack.

What You'll Need

  • A working automation setup (Selenium, Puppeteer, Playwright, or similar) with basic knowledge of your scripting language.
  • Access to the browser's command-line options or launch configuration.
  • For network-level blocking: a proxy tool like mitmproxy or AdGuard Home, or a custom request filter in your code.
  • Optional: a subscription to an ad-blocking filter list (e.g., EasyList) if using a proxy-based approach.

Step-by-Step

  1. Use browser built-in ad-blocking flags. Most automation tools let you pass command-line switches to the browser. For Chrome/Chromium, add --disable-extensions-except (if you plan to load an extension) or simply --block-new-web-contents (not an ad blocker). The most reliable flag is --disable-features=RendererCodeIntegrity (not ad-related). Instead, use --host-resolver-rules to block known ad domains by mapping them to 127.0.0.1. Example in Puppeteer: args: ['--host-resolver-rules=MAP adserver.com 127.0.0.1']. This is crude but effective for a small list.
  2. Install an ad-blocker extension in the automation browser. Download the CRX file for uBlock Origin or AdBlock Plus. In Selenium, use add_extension (Python) or addExtensions (Java). In Puppeteer, you can load a CRX via puppeteer-extra-plugin-adblocker (a plugin that uses the same filter lists). For Playwright, use playwright-extra with the adblocker plugin. This is the most practical method because it uses real filter lists.
  3. Intercept network requests in your script. In Puppeteer, enable request interception and abort requests to ad domains. Example: page.setRequestInterception(true) then check the URL against a blacklist and call request.abort(). In Playwright, use page.route('**/*', route => { if (isAd(route.request().url())) route.abort(); else route.continue(); }). In Selenium, you can use a proxy like BrowserMob to filter requests.
  4. Use a proxy that blocks ads. Run a local proxy like mitmproxy with a script that modifies responses or blocks requests to ad servers. Alternatively, set up AdGuard Home on your network and point your automation browser to its DNS. This method works across all browsers and doesn't require changing your code.
  5. Modify the browser's hosts file (for local testing). On your machine, edit the hosts file to redirect ad domains to 127.0.0.1. This is a system-wide change, so use it only on a dedicated test machine or container.
  6. Use a pre-configured browser profile. Create a browser profile with an ad-blocker installed, then launch automation with that profile. In Selenium, set options.add_argument("user-data-dir=/path/to/profile"). In Puppeteer, use userDataDir. This preserves the extension and its filter lists.

Common Mistakes

  • Relying only on flags like --disable-ads—they don't exist. Many flags are misrepresented online; always verify with official documentation.
  • Blocking too broadly. Aborting all requests to domains containing "ad" can break legitimate resources (e.g., "admin.js" or "adobe.com"). Use a curated blacklist or a filter list.
  • Forgetting to handle pop-up windows. Some ads open new tabs; your script must close them or block the window.open call.
  • Not updating filter lists. Ad domains change constantly. If you use an extension, ensure it updates automatically or manually refresh the lists.
  • Ignoring HTTPS. If you use a proxy, you must install its CA certificate in the browser to intercept HTTPS traffic. Otherwise, only HTTP ads are blocked.

Tips

  • For Selenium, the AdBlock extension works well, but you must download the CRX file from a trusted source. Use options.add_extension('path/to/ublock.crx').
  • In Puppeteer, puppeteer-extra-plugin-adblocker is the easiest way—it bundles EasyList and blocks ads out of the box. Install with npm install puppeteer-extra puppeteer-extra-plugin-adblocker.
  • For Playwright, use playwright-extra with the same plugin. Alternatively, use page.route to block requests to a list of known ad domains (e.g., from EasyList).
  • If you're scraping, consider using a headless browser with --no-sandbox flag in Docker to avoid permission issues.
  • Test your ad-blocking setup by loading a known ad-heavy page (e.g., a news site) and checking that no ad elements appear in the DOM.

Troubleshooting

  • Ads still appear. Verify that your extension is loaded correctly. In headless mode, some extensions don't work; try headed mode or use network interception instead.
  • Browser fails to start with extension. Ensure the CRX file is valid and not corrupted. Also, Chrome may block extensions in headless mode; use --headless=new (Chrome 112+) that supports extensions.
  • Script slows down. If you block requests via interception, it adds overhead. Use a proxy or DNS-level blocking for better performance.
  • HTTPS errors with proxy. Install the proxy's CA certificate in the browser's trust store. For Selenium, you can set the certificate via options.accept_insecure_certs = True (Python) but that doesn't trust the CA; you need to add it to the OS trust store.
  • Element selectors break because of missing ads. If your script relies on ad elements (unlikely), adjust selectors to target only the content you need.

Frequently Asked Questions

Can I block ads in headless Chrome?

Yes, but extension support in headless mode was limited until recently. Use --headless=new (Chrome 112+) or use network interception instead of an extension.

Will ad-blocking affect page load time?

Usually it improves load time because fewer resources are fetched. However, if you use a proxy or interception, the overhead may offset gains for very small pages.

What is the best method for Selenium?

Using an extension like uBlock Origin is the most reliable. If that fails, use a proxy like BrowserMob to filter requests.

Do I need a separate browser for each test?

No, you can reuse the same browser instance with ad-blocking enabled across multiple tests, but be aware of state leakage. Use a fresh profile if tests are isolated.

Can I block only certain types of ads (e.g., video ads)?

Yes, filter lists allow granular control. For video ads, you may need to block specific domains or use a proxy that modifies response headers.

Is it legal to block ads in automated browsers?

Yes, it's legal for personal use and testing. However, if you're scraping a website, ensure you comply with its terms of service and applicable laws.