Prompt

How do I run browser automation for a site with popups and cookie banners?

Technology / Testing & QA2 observationsLast seen Oct 27, 2026

Latest observation

Oct 27, 2026OpenAI APIWeb search: off

To run browser automation against a site that has popups and cookie banners, the usual approach is:

  1. Launch the browser
  2. Wait for the page to load
  3. Detect and dismiss cookie banners/popups
  4. Continue with the main flow
  5. 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

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (2 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.