User Agents

Web browsers and everyday web tech, explained

Fix White Screenshots in Chrome DevTools Protocol

· 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

White screenshots from Chrome DevTools Protocol (CDP) clips usually happen because the page hasn't finished painting or the capture starts before the compositor is ready. This guide shows you how to get reliable, non-white screenshots by controlling the capture timing, ensuring proper page load, and using the right CDP commands.

What You'll Need

  • Chrome or Chromium browser (recent version)
  • Access to the DevTools Protocol (via WebSocket, Puppeteer, Playwright, or raw CDP client)
  • Basic knowledge of JavaScript and async programming

Step-by-Step

  1. Ensure the page is fully loaded. Wait for the Page.loadEventFired event or use your automation library's wait-for-load function. Do not capture immediately after navigation; give the renderer time to paint.
  2. Enable the Page and Runtime domains. Send Page.enable and Runtime.enable commands before any capture. This ensures the browser is ready to emit events and handle commands.
  3. Set the device metrics override (if needed). If you're capturing a specific viewport size, send Emulation.setDeviceMetricsOverride with your desired width, height, deviceScaleFactor, and mobile flag. This prevents layout shifts and ensures the capture area is correct.
  4. Wait for the 'first paint' or 'load' event. Use Page.lifecycleEvent with the event name firstPaint or load. Alternatively, call Page.captureScreenshot only after a small delay (e.g., 100-300ms) post-load to let the compositor finish.
  5. Use the correct capture format and quality. For JPEG, set format: 'jpeg' and quality: 80 (or higher). For PNG, set format: 'png'. Avoid using fromSurface: true unless you're capturing a headless environment that requires it; in headless mode, fromSurface may be necessary but can cause white images if not supported.
  6. Capture the screenshot. Send Page.captureScreenshot with your parameters. The response will contain a base64-encoded image. Decode it and save it to a file.
  7. Handle async rendering (SPAs). If your page uses JavaScript to render content, wait for the specific element or data to appear. Use Runtime.evaluate to check for a condition, or use your library's wait-for-selector method.
  8. For full-page screenshots, use captureBeyondViewport. Set captureBeyondViewport: true and adjust the clip to the full scrollable size. This avoids white areas outside the viewport.
  9. Retry on transient failures. If you still get a white image, retry after a short delay (e.g., 500ms) and up to 3 attempts. Sometimes the compositor needs a nudge.

Common Mistakes

  • Capturing before the page has loaded – the most common cause. Always wait for the load event or a visible element.
  • Not enabling the Page domain – some CDP commands fail silently if the domain isn't enabled.
  • Using fromSurface: true in non-headless mode – this can cause white screenshots because the surface isn't available.
  • Setting device metrics after navigation – this can cause a re-layout and a white frame if captured immediately.
  • Ignoring the clip parameter – if you set a clip that extends beyond the viewport without captureBeyondViewport, you'll get white areas.

Tips

  • Use Page.captureScreenshot with format: 'png' for higher fidelity, especially for text-heavy pages.
  • For headless Chrome, add the flag --headless=new (or --headless=chrome) to use the new headless mode that supports fromSurface correctly.
  • If you're using Puppeteer, use page.screenshot() which handles many of these timing issues internally, but you can still pass waitUntil: 'networkidle0' for more reliability.
  • When capturing after animations, wait for the animation to finish using requestAnimationFrame or a timeout.
  • Test on a real page with heavy CSS to verify your timing is robust.

Troubleshooting

If you still get white screenshots, try these steps in order:

  • Check the page URL. Open the same URL in a regular browser and see if it loads correctly. Some sites block headless browsers or have anti-bot measures that result in a blank page.
  • Inspect the screenshot dimensions. If the width/height are 0, your clip or device metrics are wrong. Set them explicitly.
  • Enable verbose logging. In Chrome, launch with --enable-logging --v=1 to see any rendering errors in the console.
  • Try a different capture method. If Page.captureScreenshot fails, try Page.captureSnapshot (for MHTML) or use the HeadlessExperimental.beginFrame command to manually trigger a frame and then capture.
  • Update Chrome. Older versions have bugs with CDP screenshots. Use the latest stable Chrome or Chromium.
  • If using Puppeteer/Playwright, try their built-in screenshot methods – they have additional logic to wait for the compositor.

Frequently Asked Questions

Why does the screenshot come out white even after waiting for load?

The load event fires when the resource loading is done, but the page may still be rendering (e.g., web fonts, images, or JavaScript painting). Wait for a specific element or use a small delay after load.

Does fromSurface need to be true or false?

In headless mode, fromSurface: true is often required to capture the page. In headed mode, set it to false (the default) to capture the actual viewport. If you get a white image, try toggling this value.

How do I capture a full-page screenshot without white space?

Set captureBeyondViewport: true and provide a clip that covers the entire scrollable area. You can get the scroll height via Runtime.evaluate with document.documentElement.scrollHeight.

Can I capture a screenshot before the page is fully loaded?

Yes, but you must wait for at least the first paint. Listen for the Page.lifecycleEvent with firstPaint or use a short timeout after navigation.

Why does the screenshot work in headed mode but not headless?

Headless mode has a different rendering pipeline. Ensure you're using the new headless mode (--headless=new) and set fromSurface: true if needed.

What if the page uses WebGL or canvas?

WebGL content may not be captured via the standard screenshot method. Use HeadlessExperimental.beginFrame with screenshot parameter, or use a library that supports capturing the GPU output.