User Agents

Web browsers and everyday web tech, explained

Fix Chrome Extension Background Page Errors in Headless Selenium

· 4 min read · Extensions

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

When running Selenium tests in headless Chrome, extensions often fail to load their background pages, throwing errors like Failed to load extension or Extension background page failed to load. This guide shows you how to fix those errors by adjusting Chrome options, using the correct extension format, and handling service worker vs. background page differences.

What You'll Need

  • Chrome or Chromium installed (version 109+ for headless mode)
  • Selenium WebDriver (Python, Java, or JavaScript) set up
  • Your extension's .crx file or an unpacked extension folder
  • Basic understanding of Chrome DevTools Protocol (CDP) for debugging

Step-by-Step

  1. Use the correct headless mode. For Chrome 109+, use --headless=new instead of the old --headless. The new mode supports extensions properly. In Selenium, set this via options.add_argument("--headless=new").
  2. Load the extension as an unpacked folder. Selenium's add_extension expects a .crx file, but if your extension is unpacked, use options.add_argument("--load-extension=/path/to/folder"). This avoids crx packaging issues.
  3. Enable extensions in headless mode. Sometimes Chrome disables extensions in headless. Force-enable with options.add_argument("--enable-extensions") and options.add_argument("--disable-extensions-except=/path/to/extension").
  4. Set a stable user-data-dir. Use a dedicated profile directory: options.add_argument("--user-data-dir=/tmp/chrome-profile"). This prevents conflicts and lets the extension's background page persist.
  5. Handle service workers (Manifest V3). If your extension uses MV3, the background page is a service worker. In headless, service workers may not start automatically. Use CDP to manually start it: driver.execute_cdp_cmd("ServiceWorker.enable", {}) then call ServiceWorker.startWorker with the scope URL.
  6. Wait for the background page to load. After creating the driver, wait for the extension's service worker or background page to be ready. Use WebDriverWait to poll for a specific window handle or a JS condition.
  7. Access the background page via CDP. For MV3, get the service worker target ID via Target.getTargets and attach to it. For MV2, use driver.window_handles to find the background page window.
  8. Disable GPU and sandbox if needed. In CI environments, add --no-sandbox and --disable-gpu to avoid unrelated crashes that can also affect background pages.
  9. Check the extension's manifest. Ensure the background script path is correct and that the extension is compatible with headless (some APIs like chrome.tabs may behave differently).
  10. Use Selenium's built-in extension loading. If you have a .crx, use options.add_extension("path.crx"). But be aware that headless often fails with crx files, so prefer unpacked loading.

Common Mistakes

  • Using the old --headless flag with Chrome 109+ — it doesn't support extensions.
  • Forgetting to specify the full path to the extension folder (relative paths often fail).
  • Not waiting for the background page — the test runs before the extension initializes.
  • Assuming MV2 background pages work in headless — they do, but only in the new headless mode.
  • Ignoring the user-data-dir requirement — without it, the extension may not persist across sessions.

Tips

  • Test your extension in headed mode first to ensure it works normally.
  • Use --remote-debugging-port=9222 and inspect chrome://extensions in a regular browser to see errors.
  • For MV3, add a --disable-features=ServiceWorkerAutoPreload argument if service workers are not starting.
  • Set a reasonable timeout for the background page to load (e.g., 10 seconds).
  • If using Selenium Grid, ensure the node has the same Chrome version and extension path.

Troubleshooting

If the background page still doesn't load, try these steps:

  • Check the console logs: Use driver.get_log("browser") to see any extension errors.
  • Verify the extension is actually loaded: In headless, use CDP to query Extensions.loadUnpacked or check chrome://extensions via a new tab (if you can navigate).
  • Try a fresh profile: Delete the user-data-dir folder and rerun — corrupted profiles cause mysterious failures.
  • Switch to a different Chrome version: Some versions have known bugs with headless extensions; try an older stable or the latest beta.
  • Use the --disable-extensions flag temporarily to confirm the error is extension-related, then re-enable one by one.
  • If using a service worker, check the scope: The service worker must be registered for the extension's origin (e.g., chrome-extension:///).

Frequently Asked Questions

Why does my extension load in headed mode but not in headless?

Headless mode historically didn't support extensions at all. With the new --headless=new mode, extensions work, but you must use that flag and sometimes enable extensions explicitly.

Can I use a .crx file in headless Selenium?

Yes, but it's less reliable. Unpacked folders with --load-extension tend to work better in headless because crx packaging can fail due to missing permissions.

How do I interact with the background page's DOM?

For MV2, find the background window handle and switch to it. For MV3, use CDP to attach to the service worker target and evaluate JavaScript directly.

My extension uses a background page but I see no errors — why isn't it running?

Check if the background page is set to persistent: false (event page). In headless, it may be suspended. You can wake it by sending a message or using CDP to call Runtime.evaluate on the service worker.

Does this work with ChromeDriver and Selenium Grid?

Yes, but ensure the grid node has the same Chrome version and the extension path is accessible. Use the same Chrome options on the node.

Is there a way to debug the extension in headless mode?

Yes, use --remote-debugging-port and connect to the DevTools protocol manually. You can also capture screenshots of the extension popup, but not the background page directly.