October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Capture a Website Screenshot After Accepting Cookies in a Fresh Playwright Browser Context

A practical Playwright guide to accepting a website’s cookie prompt in a fresh context, verifying the result, and saving a screenshot.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Playwright to create a new, non-persistent browser context, open the target page, accept its consent prompt, verify that the prompt is gone, and then save the screenshot. A fresh context isolates cookies and other browser state from other contexts and does not write browsing data to disk. The consent button and the right post-click wait depend on the site.

Capture the page after accepting cookies

The example below uses Playwright with JavaScript. Install Playwright and its Chromium browser in your project, then save this as an ES module, such as capture.mjs. Replace the URL and adapt the consent locator and wait condition to the target site; the button name shown is an example, not a selector verified for a particular site.

As an Amazon Associate I earn from qualifying purchases.

import { chromium } from 'playwright';

const targetUrl = 'https://example.com';
const browser = await chromium.launch();

try {
  const context = await browser.newContext();
  try {
    const page = await context.newPage();
    await page.goto(targetUrl, { waitUntil: 'domcontentloaded' });

    const acceptButton = page.getByRole('button', {
      name: /accept all cookies/i,
    });
    await acceptButton.click();

    // Verify a meaningful post-consent condition before capturing.
    await acceptButton.waitFor({ state: 'hidden' });

    await page.screenshot({ path: 'after-consent.png', fullPage: true });
  } finally {
    await context.close();
  }
} finally {
  await browser.close();
}

The script creates a new context for this run, navigates, interacts with a button identified by accessible role and name, waits for that button to disappear, and captures the whole page. Use a condition that reflects the site’s actual behavior: some consent interfaces hide the banner, replace it, or update another visible element after a choice.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right consent interaction

Match the target site’s control

Consent UI varies by site, language, region, and consent manager. Inspect the page and use a locator that identifies the intended action, such as a button with its accessible name. Do not assume “Accept all cookies” is the label or that a button is directly available in the main page: a site may use a different control, iframe, or UI structure.

If the control is inside an iframe, locate the relevant frame and interact with its consent button there. If the UI is rendered in a shadow root, use a locator that can reach the actual control. The exact frame or selector cannot be determined without the target URL.

Wait for the result, not just the click

A successful click is not by itself proof that the desired state was applied. Wait for the banner to become hidden or for a site-specific signal that indicates consent was recorded. If the site updates asynchronously, use an explicit locator wait rather than relying on a short fixed delay. The appropriate condition is site-specific.

Decide whether to start fresh or reuse consent

Use a fresh context to test the consent flow

A non-persistent Playwright context is isolated from other contexts, including their cookies and local and session storage, and does not persist its browsing data to disk. This makes it suitable when the goal is to encounter the consent prompt in a clean session and accept it during the run. See Playwright’s browser-context isolation guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Save and restore state for repeat captures

If you want to accept consent once and use that state in a later newly created context, save the current context’s storage state and provide it when creating the next context:

// After accepting consent in the first context:
await context.storageState({ path: 'state.json' });

// In a later run:
const context = await browser.newContext({ storageState: 'state.json' });

Playwright documents storage-state reuse, including cookies and a local-storage snapshot; its API also documents an option for IndexedDB snapshots. Whether that is enough depends on where the particular site stores its consent choice. A saved state should not be treated as universal proof that consent will carry over: confirm the site’s behavior and protect the state file as browser data. See Playwright’s authentication and storage-state guide and the BrowserContext storageState API.

Choose the screenshot scope and repeatability settings

Viewport or full page

By default, a screenshot captures the visible viewport. Set fullPage: true to capture page content beyond the current viewport. Use the viewport version for what a user sees at a particular scroll position; use full-page capture when the deliverable should include the page from top to bottom. Playwright also supports screenshot style overrides. See the Playwright screenshot guide and Page screenshot API.

Keep visual comparisons consistent

For repeatable comparisons, keep the browser engine, viewport dimensions, device scale, locale, and timing conditions consistent across runs. Decide these conditions based on the comparison you are making; the correct viewport and timing are not defined for an unspecified target page.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshoot common failures

  • The locator times out: the button text may differ, the control may be in an iframe, or the banner may not have appeared. Inspect the rendered page and adjust the locator to the target’s actual UI.
  • The click succeeds but the banner remains: the clicked control may not be the intended accept action, or the interface may update asynchronously. Verify the post-click UI and wait for the expected state.
  • The prompt returns in a later run: a fresh context starts without the prior run’s browser state. If you intentionally want to reuse consent, save and restore storage state, then confirm the site stores the choice in mechanisms covered by that state.
  • The screenshot is taken too early: navigation completion does not guarantee that consent UI or post-consent content has settled. Wait for the relevant locator or another site-specific condition before capturing.
  • The screenshot omits page content: enable fullPage: true when you need the full document rather than the visible viewport.

Or skip the browser setup

ScreenshotNeo offers a one-request screenshot API, with options for formats including PNG, JPEG, WebP, or PDF. Its consent handling accepts cookie or consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses indicate the page verdict and billing status in headers. It also provides an MCP server with screenshot, page-info, and PDF tools for AI agents. See ScreenshotNeo and the API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

The call saves the response as shot.webp; provide your API key and the target URL. The free plan includes 1,000 screenshots per month without a card, and paid plans start at $5 for 3,000 shots. Sign up for 1,000 free screenshots a month, with no card required.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does a fresh Playwright context delete cookies from my regular browser?

No. A Playwright non-persistent context is separate from your regular browser profile; it does not clear that profile’s cookies.

Can I use this method with a website that has no cookie banner?

Yes. The capture can proceed without a consent click, but make the interaction conditional on the site’s UI so the script does not wait indefinitely for a button that is absent.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.