Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Register a dialog handler before the action that could open it, then make the handler explicitly accept or dismiss the dialog. For a prompt that needs input, call dialog.accept('text'). Playwright automatically dismisses dialogs when no dialog listener is registered, but once a listener exists, it must resolve every dialog or the triggering action can stall.

Handle a dialog before the action that may open it

A dialog may appear only on a particular branch or under particular data, so install the handler before clicking or otherwise triggering that branch. Playwright’s dialog guide demonstrates registering page.on('dialog', ...) before the click. The handler can remain in place if the dialog does not appear; there is no need to guess with a fixed sleep.

page.on('dialog', async dialog => {
  if (dialog.type() === 'prompt') {
    await dialog.accept('expected input');
  } else if (dialog.type() === 'confirm') {
    await dialog.dismiss();
  } else {
    await dialog.dismiss(); // Example policy for an unexpected alert.
  }
});

await page.getByRole('button', { name: 'Continue' }).click();

This is an example policy, not a universal rule. Accept or dismiss according to the application branch the test is meant to exercise. In particular, dismissing an unexpected alert is one deliberate way to keep the test from accepting an unplanned action.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose what the test should do with each dialog

Dialog Playwright action Use it when
alert await dialog.accept() or await dialog.dismiss() The test expects the alert to be acknowledged, or deliberately cancels an unexpected dialog.
confirm await dialog.accept() or await dialog.dismiss() Accept to exercise the positive/confirm branch; dismiss to exercise cancellation.
prompt await dialog.accept('text') or await dialog.dismiss() Supply the expected input and accept, or cancel the prompt.

The Dialog API exposes type(), message(), and defaultValue(), as well as accept() and dismiss(). Use those properties to distinguish conditional cases or inspect a prompt’s initial value. To test that a dialog actually appeared, assert its type or message inside the handler before resolving it: automatic dismissal alone does not verify the dialog or its content.

Why a logging-only handler can hang the test

Without a page or browser-context dialog listener, Playwright automatically dismisses dialogs. That default can be enough when an incidental dialog only needs to be cleared, but it does not accept the dialog or prove that it appeared.

Registering a listener changes the responsibility: the callback must call accept() or dismiss(). A callback that only logs dialog.message() leaves the modal dialog open, blocking page execution and potentially leaving the click or other triggering action waiting. If an action appears hung, check whether a listener is installed and whether every path through its callback resolves the dialog.

Choose page or browser-context scope

  • Page handler: use page.on('dialog', handler) when the behavior belongs to one page.
  • Context handler: use the BrowserContext dialog event when one shared policy should cover pages in that context. The API reference marks this event as added in Playwright v1.34.

For a single expected dialog, a one-time handler can make the test’s intent clear; for possible dialogs across a sequence, a persistent listener may be appropriate. In either case, install it before the trigger and ensure its behavior is safe for every dialog it can receive during its lifetime.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
async def handle_dialog(dialog):
    if dialog.type == "prompt":
        await dialog.accept("expected input")
    elif dialog.type == "confirm":
        await dialog.dismiss()
    else:
        await dialog.dismiss()

page.on("dialog", handle_dialog)
await page.get_by_role("button", name="Continue").click()

This is the Python async form; use the sync or async API that matches the test suite. The documented event-handling principle is the same across the examples.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep beforeunload and print dialogs distinct

Playwright’s dialog guide covers alert(), confirm(), prompt(), and beforeunload confirmations. A page close that should run a beforeunload handler uses page.close({ runBeforeUnload: true }). Print dialogs have separate guidance in the same guide and should not be treated as ordinary JavaScript dialogs.

The Dialog API’s core methods and properties are documented for current Playwright releases; the BrowserContext dialog event is specifically marked as introduced in v1.34. Check the API reference for the version installed in your project when depending on a version-specific feature.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.