Scraping Central is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Playwright Advanced: Handling Popups and Dialogs

Master handling JavaScript alerts, confirm dialogs, popups, and new browser windows in Playwright for reliable web scraping.

Browser Automation · #3intermediate2 min read
Share:WhatsAppLinkedIn

When scraping websites, you will frequently encounter JavaScript dialogs (alert, confirm, prompt) and popup windows. If your scraper does not handle these, it can hang indefinitely or crash. Playwright provides clean APIs to intercept and dismiss these interruptions automatically.

Handling JavaScript Dialogs

JavaScript dialogs (alert, confirm, prompt) block page execution until they are dismissed. Playwright lets you listen for dialog events and respond programmatically.

from playwright.sync_api import sync_playwright

def handle_dialog(dialog):
    print(f"Dialog type: {dialog.type}")
    print(f"Dialog message: {dialog.message}")
    dialog.accept()  # or dialog.dismiss()

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()

    # Register the dialog handler BEFORE triggering the dialog
    page.on("dialog", handle_dialog)

    page.goto("https://example.com")

    # If a button triggers an alert, click it safely
    page.click("#show-alert-button")

    browser.close()

For prompt dialogs that expect text input, you can pass a value:

def handle_prompt(dialog):
    if dialog.type == "prompt":
        dialog.accept("my input value")
    else:
        dialog.accept()

Handling Popup Windows

Some sites open links or content in new windows or tabs. Playwright can capture and interact with these popup pages.

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto("https://example.com")

    # Wait for the popup to open when clicking a link
    with page.expect_popup() as popup_info:
        page.click("a[target='_blank']")

    popup = popup_info.value
    popup.wait_for_load_state()

    print(f"Popup URL: {popup.url}")
    print(f"Popup title: {popup.title()}")

    # Scrape content from the popup
    content = popup.content()
    print(content[:500])

    popup.close()
    browser.close()

Dismissing Cookie Consent Popups

Cookie banners are not JavaScript dialogs but regular DOM elements. Handle them by clicking the accept button:

try:
    page.click("button:has-text('Accept')", timeout=3000)
except:
    pass  # No cookie banner appeared

Auto-Dismissing All Dialogs

For scraping, you often want to dismiss every dialog automatically so your script never blocks:

page.on("dialog", lambda dialog: dialog.dismiss())

When Popups Become Too Complex

Sites with aggressive popups, overlays, and consent walls can be difficult to scrape reliably with browser automation. Services like ScraperAPI and ScrapingAnt often handle these automatically, returning clean page HTML without popup interference.

Next Steps

  • Learn Playwright waiting strategies for reliable element selection
  • Handle forms, dropdowns, and complex interactions
  • Use browser contexts to manage sessions