Fix Empty Selenium Screenshots in Headless Chrome
In this article
When you run Selenium tests in headless Chrome, screenshots sometimes come back as blank or empty files. This usually happens because the browser hasn't finished rendering the page, or because the viewport size is set to zero. The fix involves forcing a stable render state, setting an explicit window size, and waiting for the page to be fully loaded before capturing. Below is a step-by-step guide to resolve this issue reliably.
What You'll Need
- A working Selenium setup (Python, Java, or JavaScript) with ChromeDriver installed.
- Chrome browser (version 60 or later for headless mode).
- Basic knowledge of your test framework's wait commands (implicit or explicit waits).
Step-by-Step
- Set the window size explicitly. In headless mode, Chrome sometimes defaults to a 0x0 viewport. Use the
window-sizeargument when creating the Chrome options. For example, in Python:options.add_argument("--window-size=1920,1080")This ensures the viewport has a real size so the screenshot has content. - Enable headless mode properly. Use the correct headless flag for your Chrome version. For Chrome 109 and earlier, use
--headless. For Chrome 110+, use--headless=new(or just--headlesswhich now uses the new mode). In Python:options.add_argument("--headless=new")The old headless mode can sometimes produce blank screenshots on newer Chrome versions. - Disable GPU acceleration. Headless Chrome often has GPU-related rendering issues. Add
--disable-gputo your options. This is especially important on Windows or when using virtual displays. - Wait for the page to be fully loaded. After navigating to a URL, don't take the screenshot immediately. Use an explicit wait for a specific element or for the document ready state to be 'complete'. In Python:
WebDriverWait(driver, 10).until(lambda d: d.execute_script("return document.readyState") == "complete")This ensures the page's initial render is done. - Add a small delay after the page load. Even after readyState is complete, some dynamic content (images, fonts, JavaScript) may still be loading. A short sleep of 1-2 seconds before the screenshot can help. Use
time.sleep(2)in Python, orThread.sleep(2000)in Java. - Scroll to the top before capturing. If the page was scrolled down, the screenshot may capture an empty area. Use
driver.execute_script("window.scrollTo(0,0)")to reset the scroll position. - Take the screenshot using the correct method. Use
driver.save_screenshot('file.png')in Python,driver.getScreenshotAs(OutputType.FILE)in Java, ordriver.takeScreenshot()in JavaScript. Ensure the file path is writable. - Verify the screenshot file size. After saving, check that the file size is greater than zero. If it's 0 bytes, the screenshot was blank. Adjust the previous steps and retry.
Common Mistakes
- Forgetting to set window size. This is the most common cause of blank screenshots in headless mode.
- Using an outdated headless flag. Chrome 110+ deprecated the old
--headlessmode; using it can cause rendering issues. - Not waiting for dynamic content. Single-page apps that load data asynchronously may not have content when the screenshot is taken.
- Taking the screenshot before navigation completes. If you call
get()and immediately screenshot, the page may not have started rendering. - Ignoring the viewport size in the test framework. Some frameworks override the window size; ensure your options are applied correctly.
Tips
- Use a fixed viewport size that matches your target device, e.g., 1366x768 for desktop.
- If your page has fixed headers or footers, they may appear in the screenshot; consider scrolling to the element you want to capture.
- For full-page screenshots, you need to set the window size to the full page height first. Use
driver.set_window_size(1920, driver.execute_script("return document.body.scrollHeight")). - Run your tests in headed mode once to confirm the page renders correctly, then switch to headless.
- Use a screenshot comparison tool to catch regressions, but ensure the baseline is taken with the same headless settings.
Troubleshooting
If the screenshot is still empty after following the steps:
- Check the ChromeDriver version. Mismatched Chrome and ChromeDriver versions can cause rendering issues. Update both to the latest stable versions.
- Try a different screenshot method. Instead of
save_screenshot, usedriver.get_screenshot_as_file()or capture as base64 and decode it. Sometimes the file I/O can be the issue. - Disable the sandbox. In some CI environments, the Chrome sandbox causes problems. Add
--no-sandboxto your options (only if you trust the environment). - Use a virtual display. If you're on Linux without a display, install
xvfband run your tests withxvfb-run. This can resolve rendering issues in headless mode. - Check for browser crashes. Add
--disable-dev-shm-usageto options if you're in a container with limited shared memory. - Test with a simple page. Navigate to a basic HTML page (like
data:text/html,) and take a screenshot. If that works, the issue is with your page's content.
Frequently Asked Questions
Why does the screenshot come out completely white?
A white screenshot usually means the viewport size is 0 or the page hasn't rendered. Setting --window-size and waiting for the page to load should fix it.
Do I need to use --headless=new?
For Chrome 110 and later, it's recommended to use --headless=new because the old headless mode is deprecated and may have rendering bugs. For older Chrome versions, the old flag works fine.
Can I take a full-page screenshot in headless mode?
Yes, but you need to set the window size to the full page height first. Use JavaScript to get document.body.scrollHeight and then set the window size accordingly before taking the screenshot.
Why does the screenshot work in headed mode but not headless?
Headless mode has different rendering defaults, such as no GPU and a smaller viewport. The fixes above—setting window size and disabling GPU—usually resolve the discrepancy.
Does using --disable-gpu affect performance?
It can slightly reduce rendering speed, but it's often necessary for stability in headless mode. The performance impact is minimal for most test scenarios.
How long should I wait before taking the screenshot?
There's no fixed time; it depends on the page. Use explicit waits for elements you expect to be visible, or wait for document.readyState to be 'complete', then add a small extra delay (1-2 seconds) for dynamic content.