Manuals / Playwright with Python / Ch 17

Part 2 · Core InteractionsIntermediate35 min read

11. Alerts, Dialogs, Popups

Playwright with Python · 244 pages source format

page.on("dialog", lambda dialog: dialog.accept()) page.get_by_role("button", name="Delete account").click() # triggers confirm() Native browser dialogs (alert(), confirm(), prompt()) block all further JavaScript execution until dismissed. You must register a handler before triggering the action that opens the dialog — if you forget, the dialog blocks the page indefinitely and your test times out w

What you'll learn

  • page.on("dialog", lambda dialog: dialog.accept()) page.get_by_role("button", nam
  • You must register a handler before triggering the action that opens the dialog —

Overview

Native browser dialogs (alert(), confirm(), prompt()) block all further JavaScript execution until dismissed. You must register a handler before triggering the action that opens the dialog — if you forget, the dialog blocks the page indefinitely and your test times out waiting.

Run / study this snippet
page.on("dialog", lambda dialog: dialog.accept())
Interactive study board
OverviewDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

page.on("dialog", handler)

What it does: Registers a persistent listener that fires whenever a native browser dialog appears.

Types/params:

Pointers: Must be registered before the triggering action, or the dialog blocks the page and the test times out.

# For prompt() dialogs — accept with a specific input value

  • "dialog" (string, required) — the event name being listened for
  • handler (function, required) — receives the dialog object as its argument
Run / study this snippet
page.on("dialog", lambda dialog: dialog.accept("my input text"))
Interactive study board
page.on("dialog", handler)Drag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

page.on("dialog", handle_dialog)

one action — if you only want to handle a single occurrence, use page.once("dialog", ...) instead.

page.on("dialog", ...) registers a persistent listener for the whole page session, not just
Interactive study board
page.on("dialog", handle_dialog)Drag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

page.once("dialog", handler)

What it does: Same as .on(), but auto-unregisters after firing once.

Types/params: Same as .on() above.

Pointers: Use when you expect/want to handle only a single dialog occurrence rather than every dialog for the rest of the session.

What it does: Accepts (clicks OK) or dismisses (clicks Cancel) the dialog.

Types/params:

Pointers: Exactly one of accept/dismiss must be called per dialog, or the page stays blocked indefinitely.

dialog.message

What it does: Read-only property exposing the dialog's displayed text.

Types/params: No parameters — read-only string property.

Pointers: Useful to log or branch logic on (e.g., accept only if the confirm text matches expected, otherwise fail intentionally).

Claude finished the response Reorganized documentation structure with granular parameter breakdowns Reorganized documentation structure with granular parameter breakdowns

  • .accept(prompt_text) → prompt_text (string, optional) — only meaningful for
  • .dismiss() → no parameters
Run / study this snippet
dialog.accept(prompt_text=None) / dialog.dismiss()
Interactive study board
page.once("dialog", handler)Drag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

Checklist