Auto-Accept Cookie Consent Banners
Automatically detect and dismiss cookie consent banners before scraping or capturing a page, so they don't block content or appear in screenshots.
- A Browserless API token from your account dashboard
Built-in consent blocker
The simplest approach is the blockConsentModals=true query parameter. It works on BQL sessions, /screenshot, /pdf, and other REST endpoints without any selector logic:
curl -X POST \
"https://production-sfo.browserless.io/screenshot?token=YOUR_API_TOKEN_HERE&blockConsentModals=true" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}' \
--output page.png
This covers most major consent platforms (OneTrust, CookieBot, GDPR tools). For sites not handled by the built-in blocker, use the custom selector approaches below.
Steps
- REST API
- Frameworks
- BQL
Use BrowserQL to load a page and automatically dismiss cookie consent banners.
- cURL
- JavaScript
- Python
1. Build the BrowserQL mutation
goto with waitUntil: networkIdle ensures the page is fully settled before the if check runs. if returns null (not an error) when the selector is absent, so the mutation completes safely on pages with no banner:
https://production-sfo.browserless.io/chromium/bql?token=YOUR_API_TOKEN_HERE
2. Send the request
curl -X POST \
"https://production-sfo.browserless.io/chromium/bql?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": "mutation AcceptCookies { goto(url: \"https://example.com\", waitUntil: networkIdle) { status } if(selector: \"[id*=accept], [class*=accept], button[id*=cookie]\", visible: true) { acceptBtn: click(selector: \"[id*=accept], [class*=accept], button[id*=cookie]\") { time } } screenshot { base64 } }"
}'
3. Check the output
The response includes a base64 screenshot of the page with the banner dismissed.
1. Send the request
import fs from 'fs';
const response = await fetch(
'https://production-sfo.browserless.io/chromium/bql?token=YOUR_API_TOKEN_HERE',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `mutation AcceptCookies {
# waitUntil: networkIdle ensures the page is fully settled before the if check runs.
# if returns null (not an error) when the selector is absent, so this is safe on
# pages with no banner.
goto(url: "https://example.com", waitUntil: networkIdle) {
status
}
if(selector: "[id*=accept], [class*=accept], button[id*=cookie]", visible: true) {
acceptBtn: click(selector: "[id*=accept], [class*=accept], button[id*=cookie]") {
time
}
}
screenshot {
base64
}
}`,
operationName: 'AcceptCookies',
}),
}
);
const { data } = await response.json();
if (data.screenshot?.base64) {
fs.writeFileSync('page.png', Buffer.from(data.screenshot.base64, 'base64'));
console.log('Screenshot saved to page.png');
}
2. Check the output
Run with node cookies.mjs. The if block clicks the accept button only when it's found on the page.
1. Install dependencies
pip install requests
2. Send the request
import base64
import requests
query = """
mutation AcceptCookies {
# waitUntil: networkIdle ensures the page is fully settled before the if check runs.
# if returns null (not an error) when the selector is absent, so this is safe on
# pages with no banner.
goto(url: "https://example.com", waitUntil: networkIdle) {
status
}
if(selector: "[id*=accept], [class*=accept], button[id*=cookie]", visible: true) {
acceptBtn: click(selector: "[id*=accept], [class*=accept], button[id*=cookie]") {
time
}
}
screenshot {
base64
}
}
"""
response = requests.post(
'https://production-sfo.browserless.io/chromium/bql?token=YOUR_API_TOKEN_HERE',
json={'query': query, 'operationName': 'AcceptCookies'},
)
data = response.json()['data']
if data.get('screenshot', {}).get('base64'):
with open('page.png', 'wb') as f:
f.write(base64.b64decode(data['screenshot']['base64']))
print('Screenshot saved to page.png')
3. Check the output
Run with python cookies.py. The page is captured after dismissing the banner.
Use a browser connection to detect and click cookie consent banners with custom selector logic.
- Puppeteer
- Playwright
1. Install dependencies
npm install puppeteer-core
2. Connect, dismiss banner, and capture
import puppeteer from 'puppeteer-core';
const browser = await puppeteer.connect({
browserWSEndpoint: 'wss://production-sfo.browserless.io?token=YOUR_API_TOKEN_HERE',
});
try {
const page = await browser.newPage();
await page.goto('https://example.com', { waitUntil: 'networkidle2' });
// Try common accept-button selectors — banners rarely share a single standard attribute.
const cookieSelectors = [
'[id*="accept"]',
'[class*="accept"]',
'button[id*="cookie"]',
'#onetrust-accept-btn-handler',
'.cc-accept',
];
for (const selector of cookieSelectors) {
try {
await page.click(selector);
console.log(`Dismissed banner with selector: ${selector}`);
await new Promise(r => setTimeout(r, 500));
break;
} catch {
// selector not found on this page — try the next one.
}
}
await page.screenshot({ path: 'page.png', fullPage: true });
console.log('Screenshot saved to page.png');
} finally {
// Always close to release the session even on error.
await browser.close();
}
3. Check the output
Run with node cookies.mjs. The screenshot shows the page content without the cookie banner overlaying it.
1. Install dependencies
npm install playwright-core
2. Connect, dismiss banner, and capture
import { chromium } from 'playwright-core';
const browser = await chromium.connectOverCDP(
'wss://production-sfo.browserless.io?token=YOUR_API_TOKEN_HERE'
);
try {
// Use the default context — browser.newPage() creates a new context that
// doesn't inherit proxy, profile, or launch settings.
const context = browser.contexts()[0];
const page = await context.newPage();
await page.goto('https://example.com', { waitUntil: 'networkidle' });
const cookieSelectors = [
'[id*="accept"]',
'[class*="accept"]',
'button[id*="cookie"]',
'#onetrust-accept-btn-handler',
'.cc-accept',
];
for (const selector of cookieSelectors) {
const el = page.locator(selector).first();
if (await el.isVisible()) {
await el.click();
console.log(`Dismissed banner with selector: ${selector}`);
await new Promise(r => setTimeout(r, 500));
break;
}
}
await page.screenshot({ path: 'page.png', fullPage: true });
console.log('Screenshot saved to page.png');
} finally {
// Always close to release the session even on error.
await browser.close();
}
3. Check the output
Run with node cookies.mjs. The screenshot shows the page content without the cookie banner overlaying it.
1. Write the mutation
goto with waitUntil: networkIdle ensures the page is fully settled before the if check runs. if returns null (not an error) when the selector is absent, so the mutation completes safely on pages with no banner. visible: true prevents clicking off-screen banner elements that are in the DOM but not rendered:
mutation AcceptCookies {
goto(url: "https://example.com", waitUntil: networkIdle) {
status
}
if(selector: "[id*=accept], [class*=accept], button[id*=cookie]", visible: true) {
acceptBtn: click(selector: "[id*=accept], [class*=accept], button[id*=cookie]") {
time
}
}
screenshot {
base64
}
}
Adjust the selector list to match the specific banner framework used on the target site (OneTrust, CookieBot, etc.).
2. Run it
Paste into the BQL IDE and click Run, or send via HTTP (see the cURL tab).
3. Check the output
{
"data": {
"goto": { "status": 200 },
"if": { "acceptBtn": { "time": 120 } },
"screenshot": { "base64": "iVBORw0KGgo..." }
}
}
Next steps
- Take a Screenshot — capture clean page screenshots
- Scrape Structured Data — extract content after banners are dismissed
- Fill and Submit a Form — automate interactions on pages with cookie gates