Fix Chrome Extension Background Page Errors in Headless Selenium
In this article
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
.crxfile or an unpacked extension folder - Basic understanding of Chrome DevTools Protocol (CDP) for debugging
Step-by-Step
- Use the correct headless mode. For Chrome 109+, use
--headless=newinstead of the old--headless. The new mode supports extensions properly. In Selenium, set this viaoptions.add_argument("--headless=new"). - Load the extension as an unpacked folder. Selenium's
add_extensionexpects a.crxfile, but if your extension is unpacked, useoptions.add_argument("--load-extension=/path/to/folder"). This avoids crx packaging issues. - Enable extensions in headless mode. Sometimes Chrome disables extensions in headless. Force-enable with
options.add_argument("--enable-extensions")andoptions.add_argument("--disable-extensions-except=/path/to/extension"). - 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. - 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 callServiceWorker.startWorkerwith the scope URL. - 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
WebDriverWaitto poll for a specific window handle or a JS condition. - Access the background page via CDP. For MV3, get the service worker target ID via
Target.getTargetsand attach to it. For MV2, usedriver.window_handlesto find the background page window. - Disable GPU and sandbox if needed. In CI environments, add
--no-sandboxand--disable-gputo avoid unrelated crashes that can also affect background pages. - Check the extension's manifest. Ensure the background script path is correct and that the extension is compatible with headless (some APIs like
chrome.tabsmay behave differently). - Use Selenium's built-in extension loading. If you have a
.crx, useoptions.add_extension("path.crx"). But be aware that headless often fails with crx files, so prefer unpacked loading.
Common Mistakes
- Using the old
--headlessflag 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=9222and inspectchrome://extensionsin a regular browser to see errors. - For MV3, add a
--disable-features=ServiceWorkerAutoPreloadargument 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.loadUnpackedor checkchrome://extensionsvia 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-extensionsflag 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.