Fix White Screenshots in Chrome DevTools Protocol
In this article
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
- Ensure the page is fully loaded. Wait for the
Page.loadEventFiredevent or use your automation library's wait-for-load function. Do not capture immediately after navigation; give the renderer time to paint. - Enable the Page and Runtime domains. Send
Page.enableandRuntime.enablecommands before any capture. This ensures the browser is ready to emit events and handle commands. - Set the device metrics override (if needed). If you're capturing a specific viewport size, send
Emulation.setDeviceMetricsOverridewith your desired width, height, deviceScaleFactor, and mobile flag. This prevents layout shifts and ensures the capture area is correct. - Wait for the 'first paint' or 'load' event. Use
Page.lifecycleEventwith the event namefirstPaintorload. Alternatively, callPage.captureScreenshotonly after a small delay (e.g., 100-300ms) post-load to let the compositor finish. - Use the correct capture format and quality. For JPEG, set
format: 'jpeg'andquality: 80(or higher). For PNG, setformat: 'png'. Avoid usingfromSurface: trueunless you're capturing a headless environment that requires it; in headless mode,fromSurfacemay be necessary but can cause white images if not supported. - Capture the screenshot. Send
Page.captureScreenshotwith your parameters. The response will contain a base64-encoded image. Decode it and save it to a file. - Handle async rendering (SPAs). If your page uses JavaScript to render content, wait for the specific element or data to appear. Use
Runtime.evaluateto check for a condition, or use your library's wait-for-selector method. - For full-page screenshots, use
captureBeyondViewport. SetcaptureBeyondViewport: trueand adjust the clip to the full scrollable size. This avoids white areas outside the viewport. - 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: truein 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
clipparameter – if you set a clip that extends beyond the viewport withoutcaptureBeyondViewport, you'll get white areas.
Tips
- Use
Page.captureScreenshotwithformat: '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 supportsfromSurfacecorrectly. - If you're using Puppeteer, use
page.screenshot()which handles many of these timing issues internally, but you can still passwaitUntil: 'networkidle0'for more reliability. - When capturing after animations, wait for the animation to finish using
requestAnimationFrameor 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=1to see any rendering errors in the console. - Try a different capture method. If
Page.captureScreenshotfails, tryPage.captureSnapshot(for MHTML) or use theHeadlessExperimental.beginFramecommand 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.