Wait for Page Load Before Headless Chrome Screenshot
In this article
When taking screenshots with headless Chrome, the browser often captures the page before all resources (images, fonts, scripts) finish loading, resulting in blank or incomplete images. This guide shows you how to reliably wait for the page to fully load before the screenshot is taken, using Puppeteer (Node.js) or Playwright. You'll learn both simple and robust methods, including waiting for network idle and specific elements.
What You'll Need
- Node.js installed (v12 or later)
- A project with Puppeteer or Playwright installed (
npm install puppeteerornpm install playwright) - Basic familiarity with JavaScript/Node.js
Step-by-Step
- Launch the browser and open a page. Use
puppeteer.launch()orchromium.launch()(Playwright) withheadless: true. Then callpage.goto(url). - Use the
waitUntiloption. Inpage.goto(), pass{ waitUntil: 'networkidle0' }(Puppeteer) or{ waitUntil: 'networkidle' }(Playwright). This waits until there are no more than 0 network connections for 500ms. For most pages, this is sufficient. - Add a fallback timeout. To avoid hanging forever, set a timeout:
page.goto(url, { waitUntil: 'networkidle0', timeout: 30000 })(30 seconds). If the page never goes idle, it will throw an error—handle it with try/catch. - Wait for a specific element (optional but recommended). After
goto(), callpage.waitForSelector('body')or a more specific selector like#main-contentto ensure the DOM is ready. This is especially useful for single-page apps. - Add a small delay for final rendering. Use
page.waitForTimeout(1000)(Puppeteer) orpage.waitForTimeout(1000)(Playwright) to give fonts and images a moment to paint. This is a pragmatic fallback when network idle isn't enough. - Take the screenshot. Call
page.screenshot({ path: 'screenshot.png', fullPage: true })orpage.screenshot({ path: 'screenshot.png' })for viewport only. - Close the browser. Use
browser.close()to free resources.
Common Mistakes
- Using
waitUntil: 'load'only. Theloadevent fires when the main HTML is parsed, but subresources like images and scripts may still be loading. This often leads to blank screenshots. - Not handling timeouts. If the page has infinite requests (e.g., live analytics),
networkidle0never fires, and your script hangs. Always set a timeout. - Forgetting to wait for dynamic content. Single-page apps render content after JavaScript executes. Waiting for network idle alone may not be enough—you need to wait for a specific element that appears after data loads.
- Taking a screenshot before the page has painted. Even after network idle, the browser may not have painted the final layout. A short delay (200-500ms) often helps.
Tips
- Prefer
networkidle0overnetworkidle2.networkidle2allows up to 2 connections, which can still mean incomplete loading. Usenetworkidle0for maximum reliability. - Combine multiple waits. For complex pages, use
waitForSelectoron a critical element, thenwaitForTimeoutfor a moment, then screenshot. - Use
page.setViewport()before navigation. This ensures the viewport size is consistent, which affects layout and screenshot dimensions. - Consider using
page.emulateMediaType('screen'). This forces print styles to be ignored, giving a more accurate screen representation. - Test with a real page. Run your script against a simple page first, then against your target to see if the wait is sufficient.
Troubleshooting
- Screenshot is still blank or partial. Increase the timeout for
goto(), add a longerwaitForTimeout(e.g., 2000ms), or usewaitForSelectoron a known element. Also check if the page uses heavy lazy-loading—you may need to scroll or trigger load events. - Script hangs indefinitely. Ensure you have a timeout on
goto()and wrap it in try/catch. If the page has continuous requests (like a live clock), usewaitUntil: 'load'plus a fixed delay instead ofnetworkidle0. - Element selector never appears. The page may be showing an error or a loading spinner. Check the page content with
page.content()or take a screenshot after a longer delay to see what's happening. - Images load but fonts don't. Fonts often load after network idle. Add a delay of at least 500ms after network idle, or use
document.fonts.readyviapage.evaluate()to wait for fonts. - Works locally but not in CI. CI environments may have slower network or different screen sizes. Increase timeouts and use a larger viewport. Also ensure the browser binary is installed correctly.
Frequently Asked Questions
What is the difference between networkidle0 and networkidle2?
networkidle0 waits until there are no network connections for 500ms. networkidle2 allows up to 2 connections. Use networkidle0 for a more complete load, but be aware it may never fire if the page has persistent connections.
Can I wait for a specific image to load before the screenshot?
Yes. Use page.evaluate() to check if an image's complete property is true, or use page.waitForFunction() to wait until document.images all have complete === true.
Why does my screenshot show a blank white page even after waiting?
This often happens when the page relies on JavaScript to render content, and the script hasn't executed yet. Wait for a DOM element that appears after the JavaScript runs, not just for network idle.
Is it better to use waitUntil: 'networkidle0' or a fixed delay?
Network idle is more deterministic because it waits for actual activity to stop. A fixed delay is a fallback. For most pages, network idle is sufficient; for pages with continuous requests, a fixed delay after load works better.
How long should I wait before taking the screenshot?
There's no one-size-fits-all. Start with networkidle0 and a 1-second delay. If the screenshot looks incomplete, increase the delay or add a wait for a specific element. Test with your target page.
Can I use this with Playwright?
Yes, Playwright has the same waitUntil option in page.goto(), but uses 'networkidle' instead of 'networkidle0'. The rest of the steps are similar.