Remove Cookie Banners from Playwright Screenshots
In this article
Cookie banners and consent pop-ups often ruin otherwise clean Playwright screenshots. This guide shows you how to hide or remove them so your captures look professional and distraction-free. You'll need a working Playwright setup (Node.js or Python) and access to the site's DOM to target the banner element.
What You'll Need
- A Playwright test or script (Node.js or Python) already installed.
- Basic knowledge of CSS selectors or XPath to locate the banner element.
- Optional: a browser extension or ad-blocker rule if the banner is injected via a third-party script.
Step-by-Step
- Identify the banner selector. Open the site in a normal browser, right-click the cookie banner, and choose Inspect. Look for a unique ID or class (e.g.,
#cookie-banner,.consent-banner). If the banner is iframed, note the iframe selector as well. - Add a wait for the banner to appear. In your Playwright script, after navigating to the page, wait for the banner selector to be visible. This ensures you don't try to hide it before it exists. Example (Node.js):
await page.waitForSelector('#cookie-banner', { state: 'visible' }); - Hide the banner with CSS. Use
page.addStyleTag()to inject CSS that setsdisplay: none !important;on the banner. This is the most reliable method because it works regardless of how the banner is styled. Example:await page.addStyleTag({ content: '#cookie-banner { display: none !important; }' }); - Alternatively, remove the element via JavaScript. If CSS hiding doesn't work (e.g., the banner re-appears after a delay), use
page.evaluate()to remove it from the DOM. Example:await page.evaluate(() => { document.querySelector('#cookie-banner')?.remove(); }); - Handle iframes. If the banner is inside an iframe, you must first get the frame and then hide/remove the element within it. Example:
const frame = page.frameLocator('#consent-iframe'); await frame.locator('.banner').evaluate(el => el.remove()); - Take the screenshot. After hiding the banner, wait a moment for any layout shifts, then capture the screenshot as usual:
await page.screenshot({ path: 'clean.png', fullPage: true }); - If the banner reappears on scroll or interaction, consider blocking it at the network level using Playwright's route interception to abort requests to known consent-management providers (e.g.,
consent.google.com,cookiebot.com). Example:await page.route('**/*consent*', route => route.abort());
Common Mistakes
- Using a hardcoded wait instead of waiting for the selector. The banner might load later than expected, so your hide code runs before it exists.
- Hiding the wrong element. Some sites have multiple overlays (e.g., a separate privacy notice and a cookie banner). Inspect carefully to target the right one.
- Forgetting
!importantin CSS. The banner's own styles may override yours without it. - Not accounting for iframes. If the banner is in an iframe, your main page selectors won't work.
- Removing the element too early. Some banners are injected dynamically after a few seconds; wait for them to appear first.
Tips
- Use a dedicated test user agent or browser context to avoid personalized consent screens.
- If you're taking many screenshots, create a helper function that hides all known banner selectors at once.
- For sites that use a consent management platform (CMP), you can often click the "Accept" button instead of hiding — but hiding is faster and doesn't change the page state.
- Take a screenshot before and after to verify the banner is gone and no layout shift occurred.
- If the banner is sticky and covers content, hiding it may cause the page to reflow. Consider using
visibility: hiddeninstead ofdisplay: noneto preserve layout.
Troubleshooting
- Banner still shows in the screenshot. Double-check the selector — it might have a different class on different pages. Use a wildcard or multiple selectors. Also verify the banner is not inside a shadow DOM.
- The banner hides but the page looks broken. This usually happens when the banner's removal causes layout shift. Try
visibility: hiddeninstead ofdisplay: none, or set a fixed height on the banner's parent. - The banner appears after a few seconds, after your screenshot. Increase the wait time or use
page.waitForTimeout()after hiding to allow any delayed scripts to run. - Network blocking doesn't work. Some consent scripts are bundled with the main JavaScript. In that case, use a combination of CSS hiding and DOM removal.
- Iframe banner cannot be located. Use
page.frames()to list all frames and find the one containing the banner. Then useframe.$()to target it.
Frequently Asked Questions
Can I remove cookie banners without changing the screenshot's content?
Yes, hiding the banner via CSS or DOM removal does not alter the rest of the page's content. The screenshot will look as if the banner never existed.
Will this work on all websites?
Most static banners can be hidden this way. However, sites with complex dynamic consent flows or those that re-inject the banner after user interaction may require more advanced handling, such as waiting for a specific state or using network interception.
Is it better to click the "Accept" button instead of hiding the banner?
Sometimes. Clicking accept can trigger additional scripts that load content behind the banner, but it may also change the page's appearance (e.g., showing personalized elements). Hiding is simpler and keeps the page in its original state.
How do I hide a banner that appears in a shadow DOM?
Use Playwright's piercing selectors. For example, page.locator('my-element').shadowRoot.locator('.banner').
Can I use this technique for PDF export or other output?
Yes, the same hiding steps work before generating a PDF with page.pdf().
What if the banner is a legal requirement to display?
This guide is for testing and development purposes only. Always comply with applicable laws and regulations when using screenshots publicly.