Fix Chrome Headless Screenshots Blank in Selenium
In this article
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
- 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 theset_window_size()method (or equivalent) before navigating to the page. For example, in Python:driver.set_window_size(1920, 1080). Do this before callingget(). - Add the
--headless=newflag (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=newinstead of just--headless. This often fixes blank screenshots immediately. - 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) - Wait for the page to fully render.
Taking a screenshot immediately afterget()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 useWebDriverWaitto wait for a specific element to be visible, or simply add a short sleep (e.g., 2 seconds) as a quick test. - Use
--force-device-scale-factor=1to avoid scaling issues.
Sometimes the device scale factor causes the screenshot to be blank. Adding this flag ensures a 1:1 pixel mapping. - Try the
--window-sizecommand-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. - 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. - Take the screenshot using the element or full-page method.
Sometimes the issue is with the viewport screenshot. Try taking a full-page screenshot usingdriver.save_screenshot('full.png')with the--force-device-scale-factor=1and a large window size. Alternatively, capture a specific element withelement.screenshot()to see if the problem is global.
Common Mistakes
- Setting window size after navigation: If you call
set_window_size()afterget(), the page may have already rendered at a smaller size, and the screenshot may still be blank or clipped. - Using
--headlesswithout thenewsuffix 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(), useget_screenshot_as_png()and save the bytes manually. Sometimes the file writing can fail silently. - Disable extensions: Sometimes installed extensions can interfere. Add
--disable-extensionsto your options. - Use
--disable-software-rasterizerin reverse: If you have this flag, remove it. If you don't, try adding--enable-software-rasterizeras a last resort. - Check your virtual display: If you're on Linux without a display, you might need to use
xvfb-runto 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.