Prompt
How do I run browser automation for a site with popups and cookie banners?
Latest observation
Oct 27, 2026
OpenAI APIWeb search: off
To run browser automation against a site that has popups and cookie banners, the usual approach is:
- Launch the browser
- Wait for the page to load
- Detect and dismiss cookie banners/popups
- Continue with the main flow
- Add retries / timeouts so automation stays stable
Common strategy
1) Use a real browser automation tool
Popular choices:
- Playwright — most reliable for modern sites
- Selenium — widely used, more manual
- Puppeteer — good for Chromium-only workflows
For popups and cookie consent, Playwright is usually the easiest.
Playwright example
Install
npm init -y
npm i -D playwright
npx playwright install
Example script
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });
// Try common cookie accept buttons
const acceptSelectors = [
'button:has-text("Accept")',
'button:has-text("I agree")',
'button:has-text("Allow all")',
'button:has-text("Agree")',
'[aria-label*="accept" i]',
'[id*="accept" i]',
'[class*="accept" i]',
];
for (const selector of acceptSelectors) {
const el = page.locator(selector).first();
if (await el.count()) {
try {
await el.click({ timeout: 2000 });
break;
} catch {}
}
}
// Handle modal popups
const closeSelectors = [
'button:has-text("Close")',
'button:has-text("No thanks")',
'button[aria-label="Close"]',
'[aria-label*="close" i]',
'[class*="close" i]',
];
for (const selector of closeSelectors) {
const el = page.locator(selector).first();
if (await el.count()) {
try {
await el.click({ timeout: 2000 });
break;
} catch {}
}
}
// Continue automation
await page.click('text=Get started');
// await browser.close();
})();
Better pattern: make “dismiss overlays” a helper
If you’re automating multiple pages, create a reusable helper that:
- checks for consent dialogs
- closes known modal popups
- ignores failures if a popup isn’t present
Example helper
async function dismissPopups(page) {
const candidates = [
'button:has-text("Accept")',
'button:has-text("I agree")',
'button:has-text("Allow all")',
'button:has-text("Close")',
'button:has-text("No thanks")',
'[aria-label*="accept" i]',
'[aria-label*="close" i]',
'button[title*="close" i]',
];
for (const selector of candidates) {
const locator = page.locator(selector).first();
try {
if (await locator.isVisible({ timeout: 1000 })) {
await locator.click({ timeout: 2000 });
await page.waitForTimeout(500);
}
} catch {}
}
}
If the banner is inside an iframe
Cookie banners are often embedded in iframes. In that case you need to search frames too:
async function clickInFrames(page, selectors) {
for (const frame of page.frames()) {
for (const selector of selectors) {
const el = frame.locator(selector).first();
try {
if (await el.isVisible({ timeout: 1000 })) {
await el.click({ timeout: 2000 });
return true;
}
} catch {}
}
}
return false;
}
Tips for reliability
- Use explicit waits instead of fixed sleeps when possible.
- Don’t assume one selector: cookie banners vary a lot.
- Run non-headless first so you can see what’s happening.
- Use screenshots/debugging if clicks fail.
- Handle overlays before interacting with page elements.
- Expect localized text (“Accept” may be “Aceptar”, “Akzeptieren”, etc.).
Practical workflow
A robust automation script often looks like:
await page.goto(url, { waitUntil: 'networkidle' });
await dismissPopups(page);
await page.waitForLoadState('domcontentloaded');
await dismissPopups(page); // sometimes banners appear late
// now do the actual task
If you want, I can help with:
- a Playwright script for your specific site
- Selenium version in Python/Java
- a helper that handles cookie banners in iframes
- a more advanced popup handler that deals with ads, login modals, and newsletter overlays