Skip to main content



Playwright can interact with the web page dialogs such as alert, confirm, prompt as well as beforeunload confirmation. For print dialogs, see Print.

alert(), confirm(), prompt() dialogs

By default, dialogs are auto-dismissed by Playwright, so you don't have to handle them. However, you can register a dialog handler before the action that triggers the dialog to either Dialog.accept() or Dialog.dismiss() it.

page.onDialog(dialog -> dialog.accept());

Page.onDialog(handler) listener must handle the dialog. Otherwise your action will stall, be it or something else. That's because dialogs in Web are modals and therefore block further page execution until they are handled.

As a result, the following snippet will never resolve:



page.onDialog(dialog -> System.out.println(dialog.message()));
page.getByRole(AriaRole.BUTTON).click(); // Will hang here

If there is no listener for Page.onDialog(handler), all dialogs are automatically dismissed.

beforeunload dialog

When Page.close() is invoked with the truthy runBeforeUnload value, the page runs its unload handlers. This is the only case when Page.close() does not wait for the page to actually close, because it might be that the page stays open in the end of the operation.

You can register a dialog handler to handle the beforeunload dialog yourself:

page.onDialog(dialog -> {
assertEquals("beforeunload", dialog.type());
page.close(new Page.CloseOptions().setRunBeforeUnload(true));

In order to assert that a print dialog via window.print was triggered, you can use the following snippet:


page.evaluate("(() => {window.waitForPrintDialog = new Promise(f => window.print = f);})()");
page.getByText("Print it!").click();


This will wait for the print dialog to be opened after the button is clicked. Make sure to evaluate the script before clicking the button / after the page is loaded.