User Agents

Web browsers and everyday web tech, explained

Switch Between Headless and Headed Chrome in Selenium

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

Switching between headless and headed Chrome in Selenium lets you run tests invisibly in CI/CD pipelines or on servers, and then flip to a visible browser when you need to debug or watch a test run. The core idea is simple: you control the mode through a single ChromeOptions object, and you can toggle it with a boolean flag or an environment variable. No code changes beyond the options setup are required.

What You'll Need

  • A working Selenium setup with the Selenium WebDriver library installed (e.g., selenium for Python, selenium-webdriver for Java, or the equivalent for your language).
  • Chrome browser installed on the machine where the tests run.
  • ChromeDriver (the browser driver) that matches your Chrome version. You can download it from the official site or use a driver manager like WebDriverManager.
  • Basic familiarity with writing Selenium scripts in your chosen language.

Step-by-Step

  1. Create a ChromeOptions instance. In your test setup, instantiate ChromeOptions. This object holds all browser-level settings, including headless mode.
  2. Define a flag for the mode. Add a boolean variable like isHeadless = true or read from an environment variable (e.g., HEADLESS). This makes switching easy without editing code each time.
  3. Apply headless conditionally. Use an if-statement: if headless is true, call options.addArguments("--headless"). For newer Chrome versions (112+), you may also add --disable-gpu to avoid some rendering artifacts, though it’s not always required. If headless is false, do nothing (or add --start-maximized for a visible window).
  4. Set any additional options. Common ones include --window-size=1920,1080 for headless (since headless has no natural window size), and --ignore-certificate-errors if you test on HTTPS with self-signed certs. These are independent of headless mode.
  5. Pass the options to the WebDriver. When creating the driver, use new ChromeDriver(options) in Java, webdriver.Chrome(options=options) in Python, or the equivalent in your language. This is the only place where the options are used.
  6. Run your tests. With headless set to true, the browser runs invisibly in the background. Set it to false, and a visible Chrome window appears during the test.
  7. Switch modes at runtime (optional). If you need to change mode mid-test (e.g., start headless, then debug a failure by re-running headed), you cannot change the mode of an already-created driver. Instead, you must quit the driver and recreate it with the new options. Wrap driver creation in a method that takes the headless flag as a parameter.

Common Mistakes

  • Forgetting to add the headless argument before driver creation. If you add it after the driver is created, it has no effect. Always set options first.
  • Using an outdated headless syntax. Older Selenium versions used --headless with a space, but modern Chrome accepts --headless=new for the new headless mode. Both work, but --headless=new is recommended for Chrome 112+.
  • Not setting a window size in headless. Headless Chrome defaults to 800x600, which can cause responsive elements to behave differently. Always set an explicit size.
  • Assuming headless is faster. Headless can be slightly faster due to no rendering, but it’s not guaranteed. Don’t rely on it for performance gains.
  • Hard-coding the mode. If you hard-code true or false, you have to edit and rerun the code every time. Use a variable or config file.

Tips

  • Use an environment variable for CI. For example, set HEADLESS=1 in your CI pipeline and read it in your test setup. This lets you run headed locally and headless in CI without code changes.
  • Add a screenshot on failure. In headless mode, you can’t see the browser, so capture screenshots or page source on failure to help debug.
  • Use the new headless mode. Chrome 112+ supports --headless=new, which behaves more like the regular browser and supports extensions and more features. It’s a drop-in replacement.
  • Keep your driver and browser versions in sync. Mismatches cause cryptic errors. Use a driver manager to automate this.
  • Test both modes periodically. Even if you run headless in CI, run a subset of tests headed to catch issues like missing UI elements that only appear when rendering.

Troubleshooting

  • Browser opens in headed mode when you expect headless. Check that the --headless argument is added before driver creation, and that your condition logic is correct (e.g., the flag is true). Also verify you’re not passing the argument twice with conflicting values.
  • Headless mode fails with a “Chrome failed to start” error. This often happens in CI environments without a display. Ensure you have the necessary system dependencies (e.g., libX11, libXcomposite) installed. Also try adding --no-sandbox if running as root (not recommended for production).
  • Elements not found in headless but found in headed. This is usually a timing or viewport issue. Increase implicit/explicit waits, and set a larger window size. Also check if the element requires hover or visibility that headless doesn’t trigger—use JavaScript to scroll into view.
  • Headless mode is slower than expected. It might be due to a lack of GPU acceleration. Add --disable-gpu (for older Chrome) or --disable-software-rasterizer to reduce overhead. Also ensure you’re not running too many parallel tests.
  • ChromeDriver version mismatch. If you see “session not created: This version of ChromeDriver only supports Chrome version X”, download the matching ChromeDriver or use a driver manager.
  • Headless mode doesn’t support file downloads. Headless Chrome has limited download support. If your test downloads files, you may need to run headed or configure download behavior with options like --download.default_directory and --download.prompt_for_download=false.

Frequently Asked Questions

Can I switch between headless and headed without restarting the browser?

No. The headless mode is set at driver creation time. To change it, you must quit the current driver and create a new one with the desired options.

Is headless mode faster than headed?

It can be slightly faster because there’s no UI rendering, but the difference is often minimal. The main benefit is that it runs without a display, which is essential in CI environments.

What is the difference between --headless and --headless=new?

The old --headless mode is the original headless implementation. --headless=new (Chrome 112+) uses the same rendering engine as the regular browser, so it supports more features like extensions and better performance. Use --headless=new if your Chrome version supports it.

Do I need to set a window size in headless mode?

Yes, it’s highly recommended. Headless Chrome defaults to 800x600, which can cause responsive layouts to behave differently. Set --window-size=1920,1080 or similar.

Can I run headless and headed tests in parallel?

Yes, you can run them in parallel as separate test suites or with separate driver instances. Just ensure each driver has its own options object.

Why does my headless test fail on a file download?

Headless Chrome has limited download support. You may need to configure download behavior via options, or run that particular test in headed mode.