ScreenshotNeo

BlogHow-to

How to Set Consent Cookies Before a Website Screenshot in Selenium

Set a site’s actual consent cookie with Selenium, verify it, then wait for the page to reflect consent before capturing a screenshot.

By the ScreenshotNeo team4 October 20267 min read

To set a consent cookie before a screenshot in Selenium, first navigate to a page on the target site’s domain, add the cookie using that site’s actual name and value, then load the page you want to capture and wait for its ready condition. Selenium does not define a universal cookie that means “consent accepted”: the name, value, scope, and sometimes payload are specific to the website or consent manager.

The example below uses consent=accepted as an illustrative placeholder only. Replace it with a cookie confirmed for the site and test context you control.

Before writing code, establish what the target site expects. In an authorized test environment, inspect the consent flow or use the site’s documented test fixture to identify the cookie name, value, and relevant attributes. A banner may depend on a structured or encoded value, multiple cookies, or a client-side state transition. A generic cookie often will not dismiss it.

Record the exact URL scope and any requirements for path, domain, secure, or sameSite. Follow the target site’s behavior; do not copy attributes blindly. Selenium’s [cookie guide](https://www.selenium.dev/documentation/webdriver/interactions/cookies/) requires you to be on the domain for which the cookie is valid before adding it.

Install Selenium and ensure a compatible browser and driver are available. This runnable example uses Selenium’s Selenium Manager support through webdriver.Chrome(). Replace the example domain, capture URL, cookie name, and value with your authorized test data.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

site_origin = "https://example.com"
target_url = "https://example.com/page-to-capture"

# Illustrative placeholder only. Use the actual consent cookie schema
# established for the target site.
consent_cookie = {
    "name": "consent",
    "value": "accepted",
    "path": "/",
}

with webdriver.Chrome() as driver:
    # Cookies can only be added in the current domain context.
    # A lightweight same-domain page, including a 404, can work.
    driver.get(site_origin + "/404")
    driver.add_cookie(consent_cookie)

    # Confirm WebDriver stored the cookie before navigating.
    stored = driver.get_cookie(consent_cookie["name"])
    if stored is None or stored["value"] != consent_cookie["value"]:
        raise RuntimeError("Consent cookie was not stored as expected")

    driver.get(target_url)

    # Replace this with a page-specific readiness condition when possible.
    WebDriverWait(driver, 20).until(
        lambda browser: browser.execute_script("return document.readyState") == "complete"
    )

    driver.save_screenshot("page.png")

document.readyState == "complete" indicates document loading finished; it does not guarantee that a single-page app, image, animation, or consent UI has reached the exact state you need. Prefer an explicit wait for a stable page element or other site-specific readiness signal when available.

The WebDriver Add Cookie command associates a cookie with the current browsing context. The required fields are name and value. Common optional fields include:

Field Use Practical note
path Limits the URL paths where the cookie applies. Defaults to / in the WebDriver protocol. Set the path the site expects.
domain Sets the cookie’s domain scope. Defaults to the current document’s domain. Add only when the target requires a specific scope; the browser’s current domain context still matters.
secure Restricts transmission to secure connections. The protocol default is false. Match the target site’s actual cookie behavior, especially for HTTPS-only state.
sameSite Controls cross-site sending behavior. Use a supported value and match the site’s expected behavior. Selenium’s Python API examples include this field.
expiry Sets an expiration time when the target cookie needs one. Use the format supported by the WebDriver binding and keep test state reproducible. A session cookie may be sufficient.

Not every browser binding accepts every cookie attribute in the same form. Consult the Selenium binding documentation and WebDriver protocol reference for the version you use. Required and optional attributes do not reveal the consent manager’s application-level schema.

4. Verify that the browser sends the intended state

Read the cookie back immediately after insertion:

cookie = driver.get_cookie("actual_consent_cookie_name")
print(cookie)

all_cookies = driver.get_cookies()
print([item for item in all_cookies if item["name"] == "actual_consent_cookie_name"])

Then navigate to the capture URL and check the rendered page. A stored cookie confirms browser storage, not that the site recognizes its value. If the banner remains, inspect whether the site requires more than one cookie, an encoded preference value, a different path or domain, or a site-side transition. This diagnosis depends on the target implementation.

5. Wait for the right page state

There is no universal sleep duration that makes every screenshot reliable. Wait for a condition tied to the page you are capturing, such as a known content element appearing or a banner disappearing:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "main")))
wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, ".consent-banner")))
driver.save_screenshot("page.png")

Use selectors that actually exist on the target site. If the banner is removed from the DOM rather than hidden, Selenium’s invisibility condition handles either case. For pages with lazy-loaded images or asynchronous content, wait for the specific content needed in the shot before capturing.

6. cURL, Python, and Node.js with ScreenshotNeo

If the goal is a clean website screenshot rather than exercising Selenium’s browser behavior, ScreenshotNeo offers a screenshot API and MCP server. Its API uses one GET request for a URL and can return PNG, JPEG, WebP, or PDF. See the ScreenshotNeo API documentation for request options.

cURL

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

Python

import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://example.com/page-to-capture"},
    timeout=90,
)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)

Node.js

const q = new URLSearchParams({
  access_key: 'YOUR_API_KEY',
  url: 'https://example.com/page-to-capture'
});
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
await Bun.write('shot.webp', res);

ScreenshotNeo accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture. Each cleanup step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers identify the page verdict and billing status. An MCP server provides take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients. Plans include 1,000 shots per month free with no card; paid plans start at $5 for 3,000 shots. Yearly billing gives two months free, and every feature is on every plan.

Create a free ScreenshotNeo account for 1,000 screenshots a month with no card.

7. Troubleshooting

Symptom Likely cause Fix
add_cookie raises an error The current page is outside the cookie’s domain context, or the cookie dictionary contains unsupported or invalid fields. Navigate to a page on the target domain first. Check the binding’s accepted fields and verify the cookie’s domain and value.
The cookie is missing after insertion The browser rejected its scope or attributes, or the requested name is wrong. Inspect get_cookie and get_cookies; adjust scope to match the target’s actual cookie.
The banner still appears The cookie name or value is only a placeholder, the consent state uses multiple or encoded values, or the site has not read the cookie yet. Confirm the site’s real schema, then navigate or refresh after insertion and wait for the page to reflect it.
The banner flashes before disappearing The target page rendered before the consent state was available, or the consent UI is initialized asynchronously. Add the cookie before opening the target URL. Wait for the expected banner or content state before taking the screenshot.
The screenshot is incomplete The document load event finished before dynamic content, fonts, images, or app rendering settled. Wait for a page-specific element or state. For lazy content, trigger or wait for the relevant content to load.
Cookie appears on one path but not another The cookie’s path or domain scope excludes the capture URL. Set the scope required by the target implementation and verify on the exact capture URL.

8. Performance, reliability, and cost

Reusing a browser session avoids repeatedly starting Chrome, but isolate cookies between tests when state could leak across cases. A fresh browser context gives more reproducible results; a same-domain lightweight page can reduce setup time when the homepage is slow. Keep waits bounded and tied to a condition so a broken page does not stall the run indefinitely.

For reliable screenshots, use deterministic test data, a fixed viewport, and a page-specific readiness condition. Cookie acceptance alone does not stabilize changing content or guarantee that remote resources loaded. Selenium’s direct browser approach has no ScreenshotNeo per-shot fee, but requires maintaining browser and driver setup and handling capture failures in your own workflow. ScreenshotNeo charges only for clean shots according to the supplied product terms; check the response’s X-Page-Verdict and X-Billed headers when auditing a request.

9. FAQ

No. WebDriver adds it in the current browsing context, so first open a page in the cookie’s valid domain context.

No. Its name and value are placeholders. Each site defines its own consent state.

Navigate to the capture URL after insertion, or refresh the current target page, so the site can read the cookie during page initialization.

No. It proves WebDriver can read the stored cookie. Confirm the rendered page reaches the expected consent state as well.

References