User Agents

Web browsers and everyday web tech, explained

Detect Headless Chrome in Selenium: Full Guide

· 4 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

Identifying whether a Chrome browser is running in headless mode during Selenium automation allows developers to adjust behavior, debug issues, or bypass detection mechanisms. This is particularly useful when testing web scraping resilience, ensuring proper rendering, or mimicking real user environments. Detecting headless mode involves inspecting browser properties, executing JavaScript within the context of the driver, and analyzing responses that differ between headless and headed modes. To do this effectively, you need a working Selenium environment with ChromeDriver properly configured.

What You'll Need

  • Selenium WebDriver for Python, Java, or another supported language
  • Google Chrome installed
  • ChromeDriver compatible with your Chrome version
  • Basic knowledge of browser automation and JavaScript execution via WebDriver

Step-by-Step

  1. Launch Chrome with Selenium using desired capabilities. In Python, initialize the driver with webdriver.Chrome(). Ensure no special arguments are passed yet unless you’re explicitly enabling headless mode for testing.
  2. Enable headless mode optionally by adding the --headless=new argument (for Chrome 109+) or --headless (older versions). This lets you test both modes.
  3. Use driver.execute_script() to run JavaScript in the browser context. Query properties known to differ in headless mode. Start with navigator.webdriver. In headless mode, this often returns true even if not using a webdriver, due to detection patches.
  4. Check navigator.plugins.length. Headless Chrome typically has no plugins, so this returns 0. In headed mode, it usually returns 3 or more (like PDF viewer, Widevine, etc.).
  5. Inspect navigator.languages. Headless browsers may return an empty array or only ["en"], while headed versions reflect system language settings.
  6. Test for CanvasRenderingContext2D.prototype.getImageData. Headless environments may have altered or missing anti-aliasing, leading to consistent pixel patterns when drawing shapes. Draw a simple gradient and compare pixel data.
  7. Use driver.execute_script("return /Headless/i.test(navigator.userAgent);"). The user agent string in headless mode often includes "HeadlessChrome", making this a quick check.
  8. Evaluate screen.width and screen.height. While not definitive, headless instances sometimes report default or minimal dimensions (e.g., 800x600), unlike real displays.
  9. Check for presence of iframe or shadow DOM inconsistencies. Some headless browsers fail to render nested DOM structures correctly. Inject a test iframe and verify contentDocument access.
  10. Log all findings and compare results between headless and headed runs. Use consistent scripts across both modes to isolate differences.

Common Mistakes

  • Assuming navigator.webdriver === true always means headless — it can be set in headed mode if automation flags are enabled.
  • Not updating ChromeDriver — outdated versions may not support --headless=new or may misreport capabilities.
  • Over-relying on a single detection method, like user agent, which can be spoofed.
  • Forgetting to remove --disable-blink-features=AutomationControlled if used, which hides automation flags and skews results.
  • Running tests on systems without GUI — some properties behave differently in CI/CD environments regardless of headless flag.

Tips

  • Combine multiple detection signals for higher accuracy. Use at least three indicators before concluding.
  • Run detection scripts in both modes to establish a baseline for comparison.
  • Use options.add_experimental_option("excludeSwitches", ["enable-automation"]) and options.add_argument("--disable-blink-features=AutomationControlled") cautiously — they alter detection but may affect test validity.
  • Consider using Puppeteer’s detection evasion techniques as a reference, since Chrome DevTools Protocol shares similarities.
  • Log JavaScript output directly from the browser console using driver.get_log("browser") to catch warnings related to headless rendering.

Troubleshooting

If detection fails or returns inconsistent results, first verify Chrome and ChromeDriver versions match. Mismatches can cause unexpected behavior. Reinstall ChromeDriver via official channels if needed. If navigator.plugins returns values in headless mode, check for arguments like --disable-plugins — these may mask real differences. If canvas tests pass unexpectedly, increase the complexity of drawn elements to expose rendering gaps. When userAgent lacks "Headless", confirm you’re using --headless=new — older flags may not modify it. In CI environments, ensure the test machine has sufficient display resolution emulation. Use --window-size=1920,1080 to standardize viewport. If all checks pass but behavior differs, consider external factors like network throttling or resource limits affecting page load.

Frequently Asked Questions

Can websites detect headless Chrome?

Yes, many sites use JavaScript checks on navigator properties, canvas fingerprinting, and plugin enumeration to identify headless browsers.

Is navigator.webdriver reliable for detection?

It’s a strong indicator but not foolproof. It can be spoofed or suppressed with additional Chrome arguments.

Does headless mode affect Selenium test accuracy?

Sometimes. Missing plugins, altered rendering, and lack of user gestures can lead to false positives or missed bugs.

How do I make headless Chrome undetectable?

You can modify navigator properties, emulate plugins, and alter canvas outputs, but this may violate site terms of service.

Why does navigator.languages return empty in headless?

Headless Chrome doesn’t inherit system language settings unless explicitly configured via --lang=en-US.

Can I detect headless mode from server-side logs?

Not directly. Detection requires client-side JavaScript execution; server logs only show user agent, which may or may not include "HeadlessChrome".