How to fix blank screenshots in headless Chrome
By the Shotfly team · Published 2026-12-01
A blank screenshot usually means the capture ran before the page painted: an app still rendering after the load event, fonts still loading, a fade-in starting at opacity: 0, or an anti-flicker snippet hiding the page. Wait for an element of the real content instead of a fixed delay, let fonts and animations finish, and check the pixels before saving.
Find the cause
| What you see | Likely cause | Fix |
|---|---|---|
| A white page, or only a spinner | A client-side app still rendering after load | Wait for a selector from the real content |
| Layout and boxes, but no text | Web fonts still loading | Wait for document.fonts.ready |
| Content in the DOM, nothing on screen | A fade-in animation, or an anti-flicker class at opacity: 0 | Finish animations; override the class |
| Grey boxes where images go | Images still loading, or lazy images below the fold | Wait for images; for long pages see full-page captures of lazy pages |
| Blank, and the DOM is empty too | A script error, a blocked request or an empty response | Read the console and the response status |
Wait for content, not for time
A fixed delay is too short on a slow day and wasted on a fast one, and networkidle never arrives on pages that keep polling. Wait for a selector that exists only once the real content is there, such as the main heading or a product grid, then run settle() and check the pixels. Save this as settle.js; the pixel check uses sharp:
import sharp from 'sharp'
// Runs inside the page, after the content you need is in the DOM.
export async function settle() {
// Anti-flicker snippets hide the page until an A/B-testing script runs; show it anyway.
const style = document.createElement('style')
style.textContent = '.async-hide { opacity: 1 !important }'
document.head.append(style)
await document.fonts.ready
// Jump fade-ins and slide-ins to their final frame. Infinite animations can't finish.
for (const animation of document.getAnimations()) {
if (animation.effect?.getComputedTiming().endTime !== Infinity) animation.finish()
}
// Wait for images that have started loading, but never longer than 5 seconds.
const pending = [...document.images].filter((img) => !img.complete && img.loading !== 'lazy')
await Promise.race([
Promise.all(pending.map((img) => new Promise((done) => (img.onload = img.onerror = done)))),
new Promise((done) => setTimeout(done, 5000)),
])
}
/** True when almost every pixel matches the top-left one: a white, black or flat page. */
export async function looksBlank(png) {
const { data, info } = await sharp(png).raw().toBuffer({ resolveWithObject: true })
const [r, g, b] = data
let same = 0
for (let i = 0; i < data.length; i += info.channels) {
if (Math.abs(data[i] - r) + Math.abs(data[i + 1] - g) + Math.abs(data[i + 2] - b) < 30) same++
}
return same / (info.width * info.height) > 0.998
}import { writeFile } from 'node:fs/promises'
import { chromium } from 'playwright'
import { looksBlank, settle } from './settle.js'
const [url, selector] = process.argv.slice(2) // selector: part of the real content, e.g. 'main h1'
const browser = await chromium.launch()
const page = await browser.newPage({ viewport: { width: 1280, height: 800 } })
await page.goto(url, { waitUntil: 'load' })
await page.waitForSelector(selector, { state: 'visible', timeout: 15_000 })
await page.evaluate(settle)
const png = await page.screenshot()
if (await looksBlank(png)) {
console.error('Blank screenshot: wait for a later selector, or check the console for errors')
process.exitCode = 1
} else {
await writeFile('screenshot.png', png)
}
await browser.close()Both libraries count an element at opacity: 0 as visible, so the selector wait alone passes on a page that is still faded out. That is why settle() also finishes animations and overrides the anti-flicker class.
Anti-flicker snippets
A/B-testing tools hide the page until their script has swapped in the variant. Some snippets add a class such as async-hide that sets opacity: 0, and remove it when the script loads or after a timeout; if the script is blocked, for example by an ad blocker, the page stays invisible until that timeout. Find the class or style in DevTools and add its selector to settle().
Check the result
looksBlank() compares every pixel with the top-left one. When more than 99.8% of them match, the image is a single flat colour: white, black or the page background. A page that is nearly empty by design, such as one small logo on white, can trip it; raise the threshold for those sites.
With Shotfly
Shotfly waits for wait_until (default load), for wait_for_selector when you set one, and for an extra delay if you ask for it. Every capture is checked for blank output before it is charged: a blank result fails with the error code blank_page and costs nothing, so retrying with a later selector doesn't pay for the empty image.
curl -G "https://api.shotfly.dev/v1/screenshot" --data-urlencode "url=https://example.com" \
--data-urlencode "wait_for_selector=main h1" \
-H "Authorization: Bearer $SHOTFLY_SECRET_KEY" -o shot.pngRelated
Last updated 2026-12-01. Markdown version: /guides/fix-blank-screenshots.md