Keep Cookie Banners Off Screenshots: A Guide
In this article
Cookie banners and consent pop-ups can ruin automated screenshots by covering page content, blocking elements, or changing the layout. This guide shows you how to prevent them from appearing in your captures—whether you're using Playwright, Puppeteer, Selenium, or a simple browser extension. You'll learn practical methods to hide, dismiss, or bypass these banners so your screenshots are clean and consistent.
What You'll Need
- Access to the browser or automation tool you're using (e.g., Playwright, Puppeteer, Selenium, or a manual browser).
- Basic knowledge of your tool's scripting language (JavaScript, Python, etc.).
- Optional: a browser extension like uBlock Origin or I don't care about cookies for manual testing.
Step-by-Step
- Identify the cookie banner's selector. Open your target website in a browser, right-click the banner, and choose "Inspect" to find its CSS selector (e.g.,
#cookie-banner,.consent-popup, ordiv[class*='cookie']). This is the element you'll target. - Use a script to hide the banner before taking the screenshot. In your automation tool, add a line to set the banner's style to
display: noneorvisibility: hidden. For example, in Playwright:page.addStyleTag({ content: '#cookie-banner { display: none !important; }' }). In Puppeteer:page.addStyleTag({ content: '.consent-popup { display: none !important; }' }). In Selenium:driver.execute_script("document.querySelector('#cookie-banner').style.display='none'"). - If the banner appears after a delay, wait for it to load first. Some banners appear after a few seconds. Use a wait command like
page.waitForSelector('#cookie-banner')in Playwright, then hide it. Alternatively, use a fixed wait (e.g.,page.waitForTimeout(2000)) before hiding. - Dismiss the banner by clicking its "Accept" or "Close" button. If hiding doesn't work (e.g., the banner is a modal that blocks interaction), click the button. In Playwright:
page.click('button:has-text("Accept")'). In Puppeteer:page.click('button:has-text("Accept")'). In Selenium:driver.find_element(By.XPATH, "//button[contains(text(),'Accept')]").click(). - Use browser profiles or extensions to block cookie banners globally. For manual screenshots or as a fallback, install an extension like "I don't care about cookies" or "uBlock Origin" with custom filters. In automation, you can launch the browser with a pre-configured profile that has these extensions installed.
- Set cookie consent via the site's API or local storage. Some sites store consent in
localStorageor a cookie. Inspect the banner's JavaScript to see what it sets (e.g.,localStorage.setItem('cookieConsent', 'true')) and set that before loading the page. In Playwright:page.addInitScript(() => localStorage.setItem('cookieConsent', 'true')). - Take the screenshot after the banner is gone. Ensure the banner is fully hidden or dismissed before capturing. Use a short delay to allow the page to reflow.
Common Mistakes
- Hiding the banner too early. If you hide it before it's inserted into the DOM, it may reappear later. Wait for the selector to be present first.
- Using a non-unique selector. A selector like
.cookiemight match multiple elements. Use a more specific one like#cookie-bannerordiv[aria-label='Cookie consent']. - Clicking the wrong button. Some banners have multiple buttons (Accept, Decline, Settings). Clicking "Settings" may open a submenu. Use a precise text match or a dedicated selector.
- Forgetting that the banner may be an iframe. Some banners are inside an iframe. You'll need to switch to the iframe context before interacting with it. In Playwright:
page.frameLocator('iframe[src*="consent"]').locator('button').click(). - Not clearing cookies between runs. If you run screenshots repeatedly, the site may remember your consent and not show the banner, but that's fine. However, if you need to test the banner's absence, ensure you don't accidentally accept it and then complain it's gone.
Tips
- Use a consistent viewport size. Set your browser window to a fixed size (e.g., 1280x720) to ensure the banner's position is predictable.
- Combine hiding and dismissing. Sometimes a banner has both a close button and a separate overlay. Hide the overlay and click the button.
- Test with a private/incognito window. This simulates a fresh visitor and ensures the banner appears, so you can verify your script works.
- Create a reusable function. If you take many screenshots, write a function that accepts a URL, hides the banner, and captures the screenshot. This saves time.
- Use browser automation's built-in screenshot options. Playwright and Puppeteer allow you to set
fullPage: trueto capture the whole page, but ensure the banner is gone first.
Troubleshooting
- The banner still appears after hiding. Check if the banner is re-inserted by JavaScript after you hide it. Use a MutationObserver to continuously hide it, or wait a bit longer and hide again. Alternatively, block the script that creates it using a network interception (e.g.,
page.route('**/cookie-banner.js', route => route.abort())). - The click on "Accept" doesn't work. The button might be covered by another element, or the text is not exactly "Accept". Try clicking by CSS selector or use a more general XPath like
//button[contains(@class, 'accept')]. - The banner is inside an iframe and you can't interact with it. Switch to the iframe context first. In Playwright:
const frame = page.frame({ url: /consent/ }); await frame.click('button');. - The site is geo-restricted and shows a different banner. Use a proxy or VPN to test from a different location, or handle multiple banner variants in your script.
- Your script works locally but not in CI. CI environments may have different timezones or locales, causing different banners. Add a fallback that hides any element with a common cookie-banner class, and run tests in a container with a fixed locale.
Frequently Asked Questions
Can I block cookie banners without using a script?
Yes, you can use browser extensions like "I don't care about cookies" or "uBlock Origin" with custom filters. These work for manual screenshots, but for automated screenshots, a script is more reliable.
Will hiding the banner affect the page layout?
Sometimes. If the banner is fixed at the top or bottom, hiding it may cause the page to expand. If that's a problem, you can also set the banner's height to 0 instead of display:none, or take the screenshot before the banner loads.
What if the site uses a cookie wall that blocks the entire page?
If the entire page is blocked until you accept, you'll need to click the accept button. Hiding the banner won't work because the page content is hidden behind it. Use the click method.
Is it legal to bypass cookie banners for screenshots?
For automated testing and internal screenshots, it's generally fine. For public screenshots, the banner is part of the site's content, but you're not violating laws by not showing it. However, avoid using this to misrepresent a site's consent requirements.
How do I handle multiple banners on one page?
Use a selector that matches all common banner elements, like [class*='cookie'], [id*='cookie'], [class*='consent']. Hide them all at once with a style tag.
Can I use this method for mobile screenshots?
Yes, the same principles apply. Just ensure your viewport is set to a mobile size and the banner's selector is still valid.