# How to take screenshots without cookie banners

> Hide or reject cookie consent banners before a headless Chrome screenshot, with Playwright and Puppeteer code, and what to do when a cookie wall blocks the page.

Hide the banner with CSS once it appears, or click the site's own **Reject** button, then take the screenshot. Hiding needs no clicks; rejecting answers the site's question, so the site removes the banner itself. Neither works on a cookie wall, where the content stays locked until someone accepts.

## Hide the banner with CSS

Many sites load their banner from a consent-management vendor, and each vendor puts it in a container with a fixed ID or class. Hide those containers, then catch home-made banners with a rule of thumb: a small fixed or sticky box that mentions cookies and contains a button. Save this as `hide-consent.js`:

```js
// Runs inside the page: hides consent banners and their dark overlays.
export function hideConsent() {
  // Containers of widely used consent managers. Check them in DevTools: vendors rename things.
  const known = [
    '#onetrust-consent-sdk', // OneTrust
    '#CybotCookiebotDialog', // Cookiebot
    '#usercentrics-root', // Usercentrics
    '.qc-cmp2-container', // Quantcast Choice
    '#didomi-host', // Didomi
    '.fc-consent-root', // Google consent messages
    '[id^="sp_message_container"]', // Sourcepoint
    '.cky-consent-container', // CookieYes
  ]
  const style = document.createElement('style')
  style.textContent = `${known.join(', ')} { display: none !important }`
  document.head.append(style)

  // Banners from no known vendor: small fixed or sticky boxes with a button that mention cookies.
  for (const el of document.body.querySelectorAll('*')) {
    const { position } = getComputedStyle(el)
    if (position !== 'fixed' && position !== 'sticky') continue
    const text = el.textContent ?? ''
    if (text.length < 1500 && /cookie|consent/i.test(text) && el.querySelector('button, [role="button"]'))
      el.style.setProperty('display', 'none', 'important')
  }
}
```

Run it after the page loads, with a short wait, because consent scripts usually insert the banner after the load event:

Playwright:

```js
import { chromium } from 'playwright'
import { hideConsent } from './hide-consent.js'

const url = process.argv[2]
const browser = await chromium.launch()
const page = await browser.newPage({ viewport: { width: 1280, height: 800 } })
await page.goto(url, { waitUntil: 'load' })
await page.waitForTimeout(1500) // consent scripts often inject the banner after load
await page.evaluate(hideConsent)
await page.screenshot({ path: 'screenshot.png' })
await browser.close()
```

Puppeteer:

```js
import puppeteer from 'puppeteer'
import { hideConsent } from './hide-consent.js'

const url = process.argv[2]
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1280, height: 800 })
await page.goto(url, { waitUntil: 'load' })
await new Promise((r) => setTimeout(r, 1500)) // consent scripts often inject the banner after load
await page.evaluate(hideConsent)
await page.screenshot({ path: 'screenshot.png' })
await browser.close()
```

The rule of thumb can also match a sticky header that links to a cookie policy and has a menu button. Check the results on a few pages of a new site, and add the site's own selector to the list when the generic rules miss it.

## Or click Reject

Clicking **Reject** gives the result a visitor who declines would see, and the site removes its own overlay. Find the button by its accessible name. The names below are English; add the wording of the sites you capture.

Playwright:

```js
import { chromium } from 'playwright'

const url = process.argv[2]
const browser = await chromium.launch()
const page = await browser.newPage({ viewport: { width: 1280, height: 800 } })
await page.goto(url, { waitUntil: 'load' })
const reject = page.getByRole('button', { name: /^(reject|decline|deny)( all)?( cookies)?$/i })
await reject.first().click({ timeout: 3000 }).catch(() => {}) // no banner, or no reject button
await page.waitForTimeout(500)
await page.screenshot({ path: 'screenshot.png' })
await browser.close()
```

Puppeteer:

```js
import puppeteer from 'puppeteer'

const url = process.argv[2]
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1280, height: 800 })
await page.goto(url, { waitUntil: 'load' })
const names = ['Reject all', 'Reject All', 'Decline all', 'Reject']
const selector = names.map((n) => `::-p-aria([name="${n}"][role="button"])`).join(', ')
const reject = await page.waitForSelector(selector, { visible: true, timeout: 3000 }).catch(() => null)
if (reject) await reject.click()
await new Promise((r) => setTimeout(r, 500))
await page.screenshot({ path: 'screenshot.png' })
await browser.close()
```

Some consent managers draw the banner inside an iframe, and these lookups don't search iframes; in Playwright, use `page.frameLocator()` for those. Hiding the container with CSS works either way.

Avoid clicking **Accept** to get a clean image. Consent is a choice for a person to make, and accepting can load trackers and ads that change the page you capture.

## Cookie walls

A cookie wall covers the whole page and offers only **Accept**, or Accept and a paid subscription. Hiding the overlay sometimes reveals the content underneath; often the content never loaded, and the screenshot shows an empty page. Detect this case and report it as a failure instead of storing a dimmed or empty page.

## With Shotfly

The `block_cookie_banners` parameter is on by default: Shotfly rejects or hides consent banners before it captures, and never clicks accept. Add `hide_selectors` for anything it misses. If a consent banner or wall is still on screen after that, the request fails with the error code `cookie_wall` and costs nothing, instead of returning a screenshot with the banner; pass `block_cookie_banners=false` to capture the page as it is.

```bash
curl -G "https://api.shotfly.dev/v1/screenshot" --data-urlencode "url=https://example.com" \
  --data-urlencode "hide_selectors=#newsletter-popup" \
  -H "Authorization: Bearer $SHOTFLY_SECRET_KEY" -o shot.png
```

## Related

- [Website screenshot API](https://shotfly.dev/use-cases/website-screenshot-api)
- [All screenshot parameters](https://shotfly.dev/docs/parameters#block_cookie_banners)
- [Error codes](https://shotfly.dev/docs/errors#cookie_wall)
- [Screenshot API comparison](https://shotfly.dev/compare)

---

Source: https://shotfly.dev/guides/screenshots-without-cookie-banners · Last updated 2026-12-01
