Selenium Chrome Headless Mode Not Working? Fixes
In this article
Selenium Chrome headless mode stops working most often because the ChromeDriver version no longer matches the installed Chrome browser, or because the headless mode arguments you're passing are outdated or conflicting. The fix is to align your ChromeDriver and Chrome versions, update your Selenium bindings, and use the current headless arguments (like --headless=new for Chrome 109+). Below, we break down why failures happen and how to resolve them step by step.
Background: Why Headless Mode Exists
Headless mode lets you run Chrome without a visible window. Selenium uses it for automated testing, scraping, and CI/CD pipelines where a GUI isn't available or desirable. It was introduced in Chrome 59 (2017) as --headless, but the implementation changed significantly in Chrome 109 (January 2023) with the new --headless=new mode. The old mode still works but is deprecated, and many scripts that worked for years suddenly break because they rely on old behavior or old flags.
How Headless Mode Works in Selenium
Selenium WebDriver communicates with ChromeDriver, which acts as a bridge between your test code and the Chrome browser. When you set chrome_options.add_argument("--headless"), ChromeDriver starts Chrome in headless mode. The browser still renders the page, executes JavaScript, and loads resources—it just doesn't paint to a screen. This is why headless mode is not a lightweight emulator; it's a full browser minus the UI.
Common reasons for failure include:
- Version mismatch between Chrome and ChromeDriver (the most frequent cause).
- Using deprecated arguments like
--disable-gpu(no longer needed in most cases). - Missing
--no-sandboxin root or container environments. - Incorrect
--user-agentor other flags that conflict with headless mode. - ChromeDriver not in PATH or not executable.
Step-by-Step Fixes for Common Failures
Follow these in order; they resolve the vast majority of issues.
- Check Chrome and ChromeDriver versions. Run
chrome --versionandchromedriver --version. They must match the major version (e.g., both 120.x). If not, download the correct ChromeDriver from the official site (or use a package manager). - Update Selenium. Use Selenium 4.6+ which includes Selenium Manager that auto-detects and downloads the right ChromeDriver. If you're on older Selenium, upgrade:
pip install -U selenium(Python) ornpm install selenium-webdriver(Node). - Use the new headless mode. For Chrome 109+, replace
--headlesswith--headless=new. In Selenium 4, you can also setoptions.add_argument("--headless=new")or useoptions.headless = True(which may default to new). - Add necessary arguments for your environment. In Linux containers or as root, add
--no-sandboxand--disable-dev-shm-usageto avoid crashes due to shared memory limits. - Remove obsolete flags.
--disable-gpuis no longer required; leaving it can cause issues on some systems. Also avoid--disable-extensionsunless you specifically need it. - Set a proper binary location. If Chrome is not in the default path, use
options.binary_location = "/path/to/chrome".
Common Problems and Their Specific Fixes
Here are the exact error messages you might see and what to do about them.
- "SessionNotCreatedException: This version of ChromeDriver only supports Chrome version 114" — Version mismatch. Download the matching ChromeDriver or let Selenium Manager handle it.
- "Chrome failed to start: exited abnormally" — Usually missing
--no-sandboxin a container or running as root. Add that flag. - "Timed out waiting for driver server to start" — ChromeDriver path is wrong or the port is blocked. Use
webdriver.Chrome(executable_path="/path/to/chromedriver")or ensure the driver is in PATH. - Blank page or no elements found — The page may detect headless mode and serve different content. Use a real user-agent and disable automation flags:
--disable-blink-features=AutomationControlledand setnavigator.webdriverto false via CDP. - High memory usage or crashes — Add
--disable-dev-shm-usagein Docker or systems with small /dev/shm.
Headless vs. Headful: When to Use Which
Headless mode is faster and uses fewer resources, but it's not identical to headful. Some sites behave differently because they detect headless via JavaScript or user-agent. If your tests pass headless but fail headful (or vice versa), it's usually due to:
- Different viewport size (headless defaults to 800x600). Set
window-size=1920,1080. - Missing GPU acceleration (headless may skip certain rendering).
- Anti-bot systems that flag headless browsers.
For critical tests, run a subset headful to verify. For CI, headless is fine, but add a small wait for network idle if you see flaky results.
Alternatives to Selenium Headless
If Selenium headless keeps giving you trouble, consider other tools that use the same Chrome DevTools Protocol (CDP):
- Puppeteer (Node.js) — Direct CDP control, often more stable for scraping.
- Playwright (Python/Node/Java) — Cross-browser, auto-waits, and has a built-in headless mode that's less finicky.
- Chrome DevTools Protocol directly — For low-level control, but you'd need to handle WebSocket connections yourself.
These tools avoid ChromeDriver version matching because they bundle their own browser binaries (Playwright) or use the installed Chrome but with better version detection (Puppeteer via puppeteer-core).
Frequently Asked Questions
Why does headless mode work on my local machine but not on the CI server?
Usually the CI server has a different Chrome version or lacks system dependencies. Ensure Chrome is installed there and that the ChromeDriver matches. Also add --no-sandbox and --disable-dev-shm-usage for Linux CI containers.
Is --headless=new better than the old --headless?
Yes. The new mode uses the same browser code as headful, so it supports extensions, better rendering, and fewer quirks. Old mode is deprecated and may be removed in future Chrome versions.
How do I take screenshots in headless mode?
Use driver.save_screenshot("file.png") (Python) or driver.get_screenshot_as_file(). It works the same as headful. Just set a proper window size first.
Can I run headless Chrome without ChromeDriver?
Not with Selenium. Selenium requires a driver to communicate with the browser. If you want to avoid ChromeDriver, use Puppeteer or Playwright which can control Chrome directly via CDP.
Why does my script work in headful but not headless?
Likely due to user-agent detection or missing flags. Set a realistic user-agent, disable automation controls, and ensure you're not relying on GPU-dependent features. Also check if the site uses JavaScript that behaves differently in headless.
What is the safest way to keep Selenium headless working long-term?
Keep Selenium and ChromeDriver updated automatically. Use Selenium Manager (built into Selenium 4.6+) or a package like webdriver-manager to fetch the correct driver each time. Also pin your Chrome version if you need reproducibility.