User Agents

Web browsers and everyday web tech, explained

Debug Selenium Scripts That Fail Only in Headless Chrome

· 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

When a Selenium script passes in headed mode but fails in headless Chrome, the root cause is almost always a difference in browser behavior, viewport size, or resource loading. This guide walks you through systematic debugging steps to identify and fix these headless-only failures.

What You'll Need

  • A Selenium test script that reproduces the failure in headless mode.
  • Chrome and ChromeDriver installed (matching versions).
  • Basic familiarity with browser DevTools and Selenium WebDriver.
  • Access to the test's logs or ability to add logging.

Step-by-Step

  1. Reproduce the failure consistently. Run the script multiple times in headless mode to confirm it's not flaky. Note the exact error message, stack trace, and the line where it fails.
  2. Compare headed vs. headless behavior. Run the same script in headed mode (remove --headless) to confirm it passes. If it also fails, the issue is not headless-specific.
  3. Set a realistic window size. Headless Chrome defaults to 800x600, which can break responsive layouts. Add options: options.add_argument('--window-size=1920,1080') and also set --start-maximized if needed.
  4. Disable headless-specific flags that cause issues. Try adding --disable-gpu (older versions), --no-sandbox (Linux CI), and --disable-dev-shm-usage (Docker). These resolve many headless-only crashes.
  5. Use explicit waits instead of implicit or fixed sleeps. Headless mode loads resources differently. Replace time.sleep() or implicit waits with WebDriverWait and expected conditions like visibility_of_element_located.
  6. Check for JavaScript errors. Headless Chrome may run JavaScript differently (e.g., missing APIs). Capture browser logs with options.set_capability('goog:loggingPrefs', {'browser': 'ALL'}) and inspect driver.get_log('browser') after the failure.
  7. Take a screenshot at failure. Wrap your script in a try/except block and call driver.save_screenshot('failure.png') to see the actual page state. Compare with a screenshot from headed mode.
  8. Inspect the DOM. Use driver.page_source or driver.find_element(By.TAG_NAME, 'body').text to see if content is missing or different.
  9. Test with different headless modes. Chrome 112+ supports --headless=new (new headless) which is closer to headed mode. Try it if you're using the old --headless.
  10. Check for user-agent differences. Headless Chrome sends a different user-agent string. Some sites serve different content or block headless. Override it with options.add_argument('--user-agent=your-normal-ua').
  11. Verify network requests. Use Selenium's performance logging or a proxy like BrowserMob to compare network calls between modes. Look for blocked resources (e.g., fonts, images) that might affect layout.
  12. Simplify and isolate. Comment out parts of the script to narrow down which action fails. Test element locators in isolation.

Common Mistakes

  • Using the same implicit wait and fixed sleeps in headless—these are unreliable.
  • Ignoring the viewport size difference.
  • Assuming headless Chrome behaves identically to headed Chrome.
  • Not capturing screenshots or logs before the browser closes.
  • Forgetting to update ChromeDriver when updating Chrome.

Tips

  • Always set a window size in headless mode.
  • Use --headless=new if your Chrome version supports it.
  • Add a unique marker to your user-agent (e.g., --user-agent=... HeadlessChrome) to test if the site treats you differently.
  • Run your tests in a Docker container with --no-sandbox and --disable-dev-shm-usage to mimic CI.
  • Use driver.set_window_size() in code instead of relying on command-line args for more control.

Troubleshooting

If the failure persists:

  • Check ChromeDriver version. Mismatched versions cause random failures. Use chromedriver --version and compare with Chrome's version.
  • Test with a fresh profile. Add --user-data-dir=/tmp/headless-profile to avoid stale cache or extensions.
  • Try a different wait strategy. Replace presence_of_element_located with visibility_of_element_located or element_to_be_clickable.
  • Check if the element is in an iframe. Switch to the iframe before interacting.
  • If the page never loads, add --disable-application-cache or --disable-extensions to rule out caching issues.
  • If you see a crash, increase memory with --disable-dev-shm-usage (especially in Docker).

Frequently Asked Questions

Why does my script fail only in headless mode?

Headless Chrome often has a smaller viewport, different user-agent, and may not load certain resources (like fonts or media) the same way. These differences can break locators or layout-dependent interactions.

How do I set the window size in headless Chrome?

Use options.add_argument('--window-size=1920,1080') or call driver.set_window_size(1920, 1080) after launching the browser.

Is there a difference between old and new headless mode?

Yes. The old headless mode (--headless) is a separate implementation, while the new mode (--headless=new) uses the same code as headed Chrome, reducing differences. Use new headless if possible.

Why do my waits time out in headless but not headed?

Headless mode may load pages slower or in a different order. Use explicit waits with WebDriverWait and increase the timeout if needed.

Can I capture a screenshot when the script fails?

Yes. Wrap your test in a try/except block and call driver.save_screenshot('screenshot.png') before quitting the driver. Also capture the page source for debugging.

Do I need to set a user-agent in headless mode?

Not always, but if the site serves different content to headless browsers, overriding the user-agent to match your normal browser can help.