User Agents

Web browsers and everyday web tech, explained

Why Headless Chrome with Selenium Returns an Empty Page

· 5 min read · Browsers

In this article
  1. What Headless Chrome Actually Does
  2. The 0x0 Window Size Problem
  3. Other Common Causes of a Blank Page
  4. How to Debug an Empty Page
  5. Comparing Headless and Headful Modes
  6. Best Practices for Reliable Headless Testing
  7. Frequently Asked Questions

Headless Chrome with Selenium most often returns an empty page because the browser window size defaults to 0x0 in headless mode, so the page renders nothing visible. This happens when you launch headless Chrome without explicitly setting window-size or using set_window_size(), and it is the single most common cause of blank screenshots or empty page_source. Below we break down the technical reasons, how to fix them, and other pitfalls that produce the same symptom.

What Headless Chrome Actually Does

Headless Chrome is the same Chromium engine as regular Chrome, but it runs without a graphical user interface. It still parses HTML, executes JavaScript, and renders the page internally—it just doesn’t paint to a screen. Selenium drives it through the ChromeDriver protocol, sending commands like get(), find_element(), and get_screenshot_as_png(). Because there is no visible window, the browser’s viewport size is not automatically set; it defaults to 800x600 in many versions, but in some configurations it can be 0x0, which causes the rendering engine to skip painting entirely.

The 0x0 Window Size Problem

When you create a headless Chrome instance without specifying a window size, ChromeDriver may report a viewport of 0x0. This is especially common on Linux servers where no X server is running. The page loads, but the layout viewport has zero dimensions, so the browser does not generate any pixels. As a result:

  • driver.page_source may contain the full HTML (because the DOM is built), but driver.get_screenshot_as_png() returns a blank or 1x1 image.
  • Elements may be considered “not visible” by Selenium because they have zero size, causing element.click() to throw an ElementNotInteractableException.
  • JavaScript that depends on window.innerWidth or window.innerHeight may behave incorrectly, leading to empty content areas.

To fix this, set the window size explicitly. In Selenium with Python:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(options=options)
driver.set_window_size(1920, 1080) # redundant but safe

In Java or C#, use the equivalent addArguments() or AddArgument(). Always set both the Chrome option and the Selenium window size to be safe.

Other Common Causes of a Blank Page

Beyond the viewport issue, several other factors can make headless Chrome return an empty page:

  • JavaScript not executed: If the page content is rendered client-side (e.g., React, Angular), the initial HTML may be nearly empty. Headless Chrome does execute JavaScript, but if the script fails due to missing APIs or network errors, the page stays blank.
  • Missing --no-sandbox on Linux: Running as root (common in Docker containers) without this flag causes Chrome to crash or hang, often leaving a blank page.
  • GPU issues: Some headless configurations try to use GPU acceleration, which fails on servers. Add --disable-gpu to avoid glitches.
  • Resource loading blocked: If the page loads external resources (CSS, JS, images) and those requests are blocked by a firewall or proxy, the page may appear blank even though the HTML is present.
  • Incorrect page_load_strategy: Setting page_load_strategy = 'none' makes Selenium return as soon as navigation begins, before the page is ready. Use 'eager' or 'normal'.

How to Debug an Empty Page

When you get a blank page, follow these steps to isolate the cause:

  1. Save the page source: with open('page.html', 'w') as f: f.write(driver.page_source). If the HTML is empty, the page didn’t load at all; if it has content but the screenshot is blank, the issue is rendering.
  2. Take a screenshot and inspect its dimensions. If it’s 1x1, the viewport is zero.
  3. Check the browser console logs: driver.get_log('browser') (Python) to see JavaScript errors.
  4. Try running the same code in non-headless mode. If it works there, the problem is headless-specific.
  5. Add a wait: WebDriverWait(driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete').

Comparing Headless and Headful Modes

Headless mode is faster and uses fewer resources because it skips painting and compositing. However, it can behave differently in subtle ways:

  • Font rendering may be different, affecting layout and element sizes.
  • Some browser features are disabled in headless (e.g., certain extensions, hardware acceleration).
  • User agent string may include “HeadlessChrome” unless you override it.
  • Window size defaults are unreliable, as discussed.

For testing, it’s often wise to run the same suite in both modes to catch discrepancies.

Best Practices for Reliable Headless Testing

To avoid empty pages and other flaky behavior:

  • Always set --window-size and --disable-gpu.
  • Use --no-sandbox and --disable-dev-shm-usage in Docker/Linux environments.
  • Set a reasonable page_load_strategy (default is normal).
  • Wait for specific elements to be present or visible before interacting.
  • If you need to emulate a mobile device, use --window-size=375,812 and set the device scale factor via --force-device-scale-factor=1.
  • Consider using --blink-settings=imagesEnabled=false to speed up if images are not needed, but be aware it may affect pages that use images for layout.

Frequently Asked Questions

Why does headless Chrome return an empty page but regular Chrome works?

The most common reason is the default window size. Regular Chrome opens with a normal viewport, while headless may start with 0x0. Setting an explicit window size fixes it. Other headless-specific issues like missing GPU or sandbox permissions can also cause blank pages.

How do I get the page source from headless Chrome if it’s empty?

First, check if the page actually loads by using driver.get('about:blank') and then navigating. If page_source is empty, the navigation may have failed. Use driver.current_url to see where you are. Also, wait for the document ready state to be complete before reading the source.

Does headless Chrome execute JavaScript?

Yes, headless Chrome runs JavaScript exactly like regular Chrome. If the page is blank, it may be because a JavaScript error occurred. Check the browser console logs with driver.get_log('browser') to see errors.

Why is my screenshot blank even though the page source has content?

This usually means the viewport size is zero or the page is not fully rendered. Set --window-size=1920,1080 and wait for the page to be fully loaded before taking a screenshot. Also, ensure you are not using page_load_strategy='none'.

Is there a way to make headless Chrome behave exactly like regular Chrome?

Not perfectly, but you can minimize differences by setting the same viewport size, user agent, and disabling GPU. Also, use the same Chrome version for both. Some differences remain due to the lack of a physical display, but for most testing purposes, it’s close enough.

Can I use headless Chrome for web scraping without getting empty pages?

Yes, if you set the correct options and wait for content to load. Many sites use JavaScript, so you need to wait for specific elements. Also, be respectful of the site’s terms of service and robots.txt.