Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Chrome Headless Screenshots Blank in Selenium

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

If you're using Selenium with Chrome in headless mode and your screenshots come back as blank white or black images, you're not alone. This is a common issue caused by how Chrome handles rendering in headless mode, especially with window size, GPU settings, or timing. This guide walks you through the most effective fixes, from simple tweaks to more robust solutions, so you can reliably capture screenshots.

What You'll Need

  • A working Selenium setup with ChromeDriver (any recent version)
  • Basic knowledge of your programming language (Python, Java, JavaScript, etc.)
  • Access to the code where you take screenshots

Step-by-Step

  1. Set a proper window size before taking the screenshot.
    In headless mode, Chrome often defaults to a tiny viewport (like 800x600) or even 0x0, which results in a blank image. Set an explicit window size using the set_window_size() method (or equivalent) before navigating to the page. For example, in Python: driver.set_window_size(1920, 1080). Do this before calling get().
  2. Add the --headless=new flag (for Chrome 109+).
    Older headless mode (sometimes called "headless old") has rendering quirks. The new headless mode is more consistent with the regular browser. In your ChromeOptions, add --headless=new instead of just --headless. This often fixes blank screenshots immediately.
  3. Disable GPU acceleration.
    In some environments, especially virtual machines or servers without a GPU, Chrome's GPU process can cause rendering issues. Add the following options to your ChromeOptions:
    --disable-gpu
    --no-sandbox (if running as root or in a container)
    --disable-dev-shm-usage (on Linux, to avoid shared memory issues)
  4. Wait for the page to fully render.
    Taking a screenshot immediately after get() can capture the page before it paints. Use an explicit wait for an element or for the document ready state to be 'complete'. In Selenium, you can use WebDriverWait to wait for a specific element to be visible, or simply add a short sleep (e.g., 2 seconds) as a quick test.
  5. Use --force-device-scale-factor=1 to avoid scaling issues.
    Sometimes the device scale factor causes the screenshot to be blank. Adding this flag ensures a 1:1 pixel mapping.
  6. Try the --window-size command-line argument.
    Instead of setting the window size in code, you can pass it directly to Chrome via options: --window-size=1920,1080. This is especially useful if you're using a remote WebDriver or a service like Selenium Grid.
  7. If using headless mode with --disable-software-rasterizer, remove it.
    Some developers add this flag to improve performance, but it can prevent the page from being drawn. Remove it if present.
  8. Take the screenshot using the element or full-page method.
    Sometimes the issue is with the viewport screenshot. Try taking a full-page screenshot using driver.save_screenshot('full.png') with the --force-device-scale-factor=1 and a large window size. Alternatively, capture a specific element with element.screenshot() to see if the problem is global.

Common Mistakes

  • Setting window size after navigation: If you call set_window_size() after get(), the page may have already rendered at a smaller size, and the screenshot may still be blank or clipped.
  • Using --headless without the new suffix on modern Chrome: The old headless mode is deprecated and can cause rendering inconsistencies.
  • Not disabling GPU on servers: On Linux servers without a display, GPU acceleration often causes blank screenshots. Always add --disable-gpu.
  • Ignoring the need to wait for dynamic content: If your page loads content via JavaScript, the screenshot may be taken before the content appears. Always use explicit waits.
  • Using a very small window size: A window size of 0x0 or even 800x600 may be too small for the page to render correctly. Use at least 1366x768.

Tips

  • Combine flags for maximum reliability: A robust set of ChromeOptions for headless screenshots is: --headless=new, --disable-gpu, --no-sandbox, --disable-dev-shm-usage, --window-size=1920,1080, --force-device-scale-factor=1.
  • Use a consistent viewport: Always set the window size both in code and via command-line to avoid conflicts.
  • Test with a simple page first: If you're still getting blanks, try screenshotting data:text/html,<h1>Test</h1> to isolate whether the issue is with your site or with Chrome itself.
  • Check the screenshot file size: A blank image is usually a few hundred bytes. If your screenshot is larger, it likely contains content.
  • Consider using a headless browser alternative: If Chrome continues to be problematic, Firefox in headless mode (via GeckoDriver) or Playwright with Chromium can be more reliable for screenshots.

Troubleshooting

If you've followed the steps and still get blank screenshots, try these additional checks:

  • Update Chrome and ChromeDriver: Version mismatches are a common cause of weird behavior. Ensure both are the same major version.
  • Check for errors in the console: Run your script in non-headless mode (remove --headless) and see if the page loads correctly. If it doesn't, the issue is with the page itself, not headless mode.
  • Try a different screenshot method: Instead of save_screenshot(), use get_screenshot_as_png() and save the bytes manually. Sometimes the file writing can fail silently.
  • Disable extensions: Sometimes installed extensions can interfere. Add --disable-extensions to your options.
  • Use --disable-software-rasterizer in reverse: If you have this flag, remove it. If you don't, try adding --enable-software-rasterizer as a last resort.
  • Check your virtual display: If you're on Linux without a display, you might need to use xvfb-run to run your script, even in headless mode, to provide a virtual framebuffer.

Frequently Asked Questions

Why is my Chrome headless screenshot blank but the page loads fine in normal mode?

Headless mode has different rendering defaults, especially around GPU usage and window size. The page may not trigger a paint event, so the screenshot captures an empty frame. Setting a window size and disabling GPU usually fixes this.

Do I need to use --headless=new?

For Chrome 109 and later, yes. The old headless mode is deprecated and has known rendering bugs. Using the new mode aligns behavior with the regular browser.

What is the best window size for screenshots?

It depends on the page, but 1920x1080 is a safe default for desktop sites. For mobile, use 375x812 (iPhone X) or similar.

Can I take a full-page screenshot in headless mode?

Yes, but you need to set a large window size or use a library like Selenium's fullpage_screenshot (not built-in). A common workaround is to set the window height to the page's scroll height.

Why does the screenshot work sometimes but not always?

This often indicates a timing issue. The page may load asynchronously, and sometimes the screenshot is taken before the content is painted. Use explicit waits for a specific element to ensure the page is ready.

Is there a way to debug without seeing the browser?

Yes, you can take a screenshot of the page in normal mode to compare. Or, you can use --remote-debugging-port=9222 and connect to the browser via DevTools Protocol to inspect the page state.