User Agents

Web browsers and everyday web tech, explained

Selenium Can't Start Chrome Headless: Fixes

· 5 min read · Browsers

In this article
  1. Why Headless Chrome Fails with Selenium
  2. How Selenium Launches Chrome (and Where It Breaks)
  3. Common Error Messages and What They Mean
  4. How to Fix Headless Chrome Startup Failures
  5. Headless vs. Headful: Which to Use for Testing
  6. Alternatives to Headless Chrome with Selenium
  7. Frequently Asked Questions

Selenium cannot start Chrome in headless mode when the browser binary is missing, the driver version mismatches Chrome, or the headless flag is passed incorrectly; the fix is to align ChromeDriver with your Chrome version, use the correct headless argument, and ensure all Chrome dependencies are installed on the system.

Why Headless Chrome Fails with Selenium

Headless mode runs Chrome without a visible window, which is ideal for automated testing and scraping. Selenium controls Chrome through ChromeDriver, a separate executable that acts as a bridge. When headless mode fails, the root cause is almost always one of three things: ChromeDriver and Chrome versions are incompatible, the headless argument is malformed, or the environment lacks libraries required by Chrome. A mismatch in versions is the most common—ChromeDriver is version-specific, and using an older driver with a newer Chrome (or vice versa) causes immediate errors like session not created or Chrome failed to start.

How Selenium Launches Chrome (and Where It Breaks)

Selenium sends commands to ChromeDriver via the WebDriver protocol. ChromeDriver then locates the Chrome binary (usually in /usr/bin/google-chrome on Linux or C:\Program Files\Google\Chrome\Application\chrome.exe on Windows) and starts it with the options you provided. In headless mode, the --headless flag is passed to the browser. The process breaks at three points:

  • Driver discovery: Selenium looks for ChromeDriver in your PATH or via Service configuration. If not found, it throws WebDriverException: 'chromedriver' executable needs to be in PATH.
  • Binary location: ChromeDriver must find the Chrome executable. If it can't, you get unknown error: cannot find Chrome binary.
  • Startup flags: Invalid or conflicting arguments can cause Chrome to exit immediately. For example, using --headless with --no-sandbox incorrectly on Windows may trigger a sandbox error.

Common Error Messages and What They Mean

Knowing the exact error text saves debugging time. Here are typical messages you'll see:

  • SessionNotCreatedException: This version of ChromeDriver only supports Chrome version XX — version mismatch.
  • WebDriverException: unknown error: Chrome failed to start: exited abnormally — Chrome crashed, often due to missing dependencies or sandbox issues.
  • WebDriverException: unknown error: cannot find Chrome binary — Chrome isn't installed or the path isn't set.
  • WebDriverException: Message: Service chromedriver unexpectedly exited — ChromeDriver crashed, often due to a corrupted file or incompatible OS.

Each message points to a specific fix. For instance, the version error is resolved by downloading the matching ChromeDriver from the official site (chromedriver.chromium.org) or using a package manager like webdriver-manager.

How to Fix Headless Chrome Startup Failures

Follow these steps in order, testing after each one:

  1. Check Chrome and ChromeDriver versions. Run google-chrome --version (or check chrome://version in the browser). Then run chromedriver --version. The major version numbers must match (e.g., Chrome 120 and ChromeDriver 120). If not, download the correct ChromeDriver.
  2. Use the correct headless argument. For Chrome 109 and earlier, use --headless. For Chrome 110+, you can use --headless=new for the new headless mode, or stick with --headless (which still works but may have limitations). In Selenium, set it via options.add_argument('--headless').
  3. Add necessary options for Linux servers. If running on a headless Linux server, add --no-sandbox, --disable-dev-shm-usage, and --disable-gpu (the last one is optional). These avoid common crashes in containerized environments.
  4. Set the binary location explicitly. Use options.binary_location = '/path/to/chrome' if Chrome isn't in the default location.
  5. Install missing dependencies. On Linux, Chrome needs libraries like libnss3, libx11-xcb1, and libgbm1. Install them with your package manager (e.g., sudo apt-get install -y libnss3 libx11-xcb1 libgbm1).
  6. Use a driver manager. Libraries like webdriver-manager (Python) or selenium-manager (built into Selenium 4.6+) automatically download the correct driver version. For Selenium 4.6+, you don't need to manually set the driver path; just ensure Chrome is installed.

Headless vs. Headful: Which to Use for Testing

Headless mode is faster and uses fewer resources, making it ideal for CI/CD pipelines and large test suites. However, it doesn't fully replicate user behavior—some sites detect headless mode and serve different content, and certain browser features (like video playback) may behave differently. Headful mode is better for debugging visual issues or when you need to interact with a real user interface. For most automation tasks, headless is sufficient, but if you encounter site-specific issues, switching to headful (by removing the --headless flag) can help isolate the problem.

Alternatives to Headless Chrome with Selenium

If Chrome headless continues to give you trouble, consider these alternatives:

  • Chrome for Testing (CfT): A dedicated Chrome build for testing, available from Google's CfT site. It's versioned to match ChromeDriver, eliminating mismatch issues.
  • Firefox headless: Selenium supports Firefox with options.add_argument('-headless'). Firefox uses GeckoDriver, which is updated independently.
  • Playwright or Puppeteer: These frameworks have built-in browser management and are often more reliable for headless automation. They download compatible browser versions automatically.

Each has trade-offs. Playwright and Puppeteer are more modern and easier to set up, but Selenium is still preferred if you need to support multiple browsers or legacy codebases.

Frequently Asked Questions

Why do I get "Chrome failed to start: exited abnormally" even with the right version?

This usually happens on Linux servers because Chrome can't create a sandbox or shared memory. Add --no-sandbox and --disable-dev-shm-usage to your options. In Docker containers, also ensure you have the --cap-add=SYS_ADMIN or run with --privileged if needed, though the flags usually suffice.

Does headless mode work on Windows without extra setup?

Yes, generally. Windows doesn't require the extra flags like --no-sandbox (which is for Linux). Just ensure ChromeDriver is in PATH and versions match. If you see a sandbox error on Windows, it's rare and often due to antivirus interference.

Is --headless=new better than --headless?

For Chrome 110+, --headless=new uses the same code as headful mode, so it supports more features (like extensions and full page rendering). The old headless mode is simpler and faster but may miss some features. If you need full compatibility, use --headless=new.

Can I run headless Chrome without installing ChromeDriver manually?

Yes. Selenium 4.6+ includes Selenium Manager, which automatically downloads the correct ChromeDriver when you create a WebDriver instance. Just install Selenium and ensure Chrome is installed; the driver is fetched automatically.

Why does headless Chrome work locally but fail on a CI server?

CI servers often run minimal Linux images lacking Chrome dependencies. Install the required libraries (libnss3, libx11-xcb1, libgbm1, etc.) and use the flags mentioned above. Also, ensure Chrome is actually installed in the CI environment—some images only have ChromeDriver.

Does headless mode affect website behavior?

Some websites detect headless browsers via JavaScript and serve different content or block access. This is not a Selenium issue but a site-specific anti-bot measure. You can try using headful mode or modify the user agent, though that may violate the site's terms of service.