Menu

Cleaning up web pages for screenshots — a practical Puppeteer guide
🖼️

Cleaning up web pages for screenshots — a practical Puppeteer guide

DEV Community·PetrDev·6 months ago
#xzWmXDid
#puppeteer#automation#node#tutorial#await
Reading 0:00
15s threshold

If you've ever taken a screenshot of a real-world webpage with Puppeteer, you've seen the ugly truth: cookie banners, sticky headers frozen at weird positions, collapsed FAQ accordions, and "Subscribe!" popups.

Here are four small Puppeteer helpers I use to get clean, presentable screenshots. Each is one function, drop-in ready.

1. Kill cookie banners

async function hideCookieBanners ( page , customSelectors = []) { await page . evaluate (( selectors ) => { // Known cookie-banner elements [ ' CookieReportsPanel ' , ' CookieReportsOverlay ' , ' CookieReportsBannerAZ ' ] . forEach ( id => document . getElementById ( id )?. remove ());

document . querySelectorAll ( ' [class*="wscr"],[id*="CookieReport"] ' ) . forEach ( el => el . remove ());

// Reset body scroll locks the banner often sets document . body . style . overflow = '' ; document . documentElement . style . overflow = '' ;

// User-defined selectors (if they know their site) selectors . forEach ( sel => { try { document .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.