2021-01-16 14:37:13 -08:00
---
id: dialogs
title: "Dialogs"
---
2023-10-06 15:08:51 +02:00
## Introduction
2024-06-19 18:04:22 +02:00
Playwright can interact with the web page dialogs such as [`alert` ](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert ), [`confirm` ](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm ), [`prompt` ](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt ) as well as [`beforeunload` ](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event ) confirmation. For print dialogs, see [Print ](#print-dialogs ).
2021-01-16 14:37:13 -08:00
## alert(), confirm(), prompt() dialogs
2022-09-07 12:47:10 +02:00
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 [`method: Dialog.accept` ] or [`method: Dialog.dismiss` ] it.
2021-01-16 14:37:13 -08:00
```js
page.on('dialog', dialog => dialog.accept());
2022-10-03 17:02:46 -07:00
await page.getByRole('button').click();
2021-01-16 14:37:13 -08:00
```
2021-03-01 09:18:44 -08:00
```java
page.onDialog(dialog -> dialog.accept());
2022-11-30 13:46:33 -08:00
page.getByRole(AriaRole.BUTTON).click();
2021-03-01 09:18:44 -08:00
```
2021-01-16 14:37:13 -08:00
```python async
page.on("dialog", lambda dialog: dialog.accept())
2022-10-03 17:02:46 -07:00
await page.get_by_role("button".click())
2021-01-16 14:37:13 -08:00
```
```python sync
page.on("dialog", lambda dialog: dialog.accept())
2022-10-03 17:02:46 -07:00
page.get_by_role("button").click()
2021-01-16 14:37:13 -08:00
```
2021-05-15 14:02:07 -07:00
```csharp
2024-03-06 17:51:44 +01:00
Page.Dialog += async (_, dialog) =>
{
await dialog.AcceptAsync();
};
await Page.GetByRole(AriaRole.Button).ClickAsync();
2021-05-15 14:02:07 -07:00
```
2021-01-16 14:37:13 -08:00
:::note
2022-09-07 12:47:10 +02:00
[`event: Page.dialog` ] listener **must handle** the dialog. Otherwise your action will stall, be it [`method: Locator.click` ] or something else. That's because dialogs in Web are modals and therefore block further page execution until they are handled.
2021-01-16 14:37:13 -08:00
:::
2022-09-07 12:30:31 +02:00
As a result, the following snippet will never resolve:
2021-01-16 14:37:13 -08:00
2021-03-11 08:42:31 -08:00
:::warning
2021-01-16 14:37:13 -08:00
WRONG!
:::
```js
2021-02-03 10:34:45 -08:00
page.on('dialog', dialog => console.log(dialog.message()));
2022-10-03 17:02:46 -07:00
await page.getByRole('button').click(); // Will hang here
2021-01-16 14:37:13 -08:00
```
2021-03-01 09:18:44 -08:00
```java
page.onDialog(dialog -> System.out.println(dialog.message()));
2022-11-30 13:46:33 -08:00
page.getByRole(AriaRole.BUTTON).click(); // Will hang here
2021-03-01 09:18:44 -08:00
```
2021-01-16 14:37:13 -08:00
```python async
2021-02-03 10:34:45 -08:00
page.on("dialog", lambda dialog: print(dialog.message))
2022-10-03 17:02:46 -07:00
await page.get_by_role("button").click() # Will hang here
2021-01-16 14:37:13 -08:00
```
```python sync
2021-02-03 10:34:45 -08:00
page.on("dialog", lambda dialog: print(dialog.message))
2022-10-03 17:02:46 -07:00
page.get_by_role("button").click() # Will hang here
2021-01-16 14:37:13 -08:00
```
2021-05-15 14:02:07 -07:00
```csharp
page.Dialog += (_, dialog) => Console.WriteLine(dialog.Message);
2022-11-30 13:46:33 -08:00
await page.GetByRole(AriaRole.Button).ClickAsync(); // Will hang here
2021-05-15 14:02:07 -07:00
```
2021-02-03 10:34:45 -08:00
:::note
If there is no listener for [`event: Page.dialog` ], all dialogs are automatically dismissed.
:::
2021-01-16 14:37:13 -08:00
## beforeunload dialog
2022-09-07 12:30:31 +02:00
When [`method: Page.close` ] is invoked with the truthy [`option: runBeforeUnload` ] value, the page runs its unload handlers. This is the only case when [`method: 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.
2021-01-16 14:37:13 -08:00
2022-09-07 12:30:31 +02:00
You can register a dialog handler to handle the `beforeunload` dialog yourself:
2021-01-16 14:37:13 -08:00
```js
page.on('dialog', async dialog => {
assert(dialog.type() === 'beforeunload');
await dialog.dismiss();
});
2022-07-12 22:39:31 +02:00
await page.close({ runBeforeUnload: true });
2021-01-16 14:37:13 -08:00
```
2021-03-01 09:18:44 -08:00
```java
page.onDialog(dialog -> {
assertEquals("beforeunload", dialog.type());
dialog.dismiss();
});
2021-03-05 13:50:34 -08:00
page.close(new Page.CloseOptions().setRunBeforeUnload(true));
2021-03-01 09:18:44 -08:00
```
2021-01-16 14:37:13 -08:00
```python async
async def handle_dialog(dialog):
assert dialog.type == 'beforeunload'
await dialog.dismiss()
page.on('dialog', lambda: handle_dialog)
await page.close(run_before_unload=True)
```
```python sync
def handle_dialog(dialog):
assert dialog.type == 'beforeunload'
dialog.dismiss()
page.on('dialog', lambda: handle_dialog)
page.close(run_before_unload=True)
```
2021-05-15 14:02:07 -07:00
```csharp
2024-03-06 17:51:44 +01:00
Page.Dialog += async (_, dialog) =>
2021-05-15 14:02:07 -07:00
{
2021-12-13 18:41:06 -08:00
Assert.AreEqual("beforeunload", dialog.Type);
2024-03-06 17:51:44 +01:00
await dialog.DismissAsync();
2021-05-15 14:02:07 -07:00
};
2024-03-06 17:51:44 +01:00
await Page.CloseAsync(new() { RunBeforeUnload = true });
2021-05-15 14:02:07 -07:00
```
2024-06-19 18:04:22 +02:00
## Print dialogs
In order to assert that a print dialog via [`window.print` ](https://developer.mozilla.org/en-US/docs/Web/API/Window/print ) was triggered, you can use the following snippet:
```js
await page.goto('< url > ');
await page.evaluate('(() => {window.waitForPrintDialog = new Promise(f => window.print = f);})()');
await page.getByText('Print it!').click();
await page.waitForFunction('window.waitForPrintDialog');
```
```java
page.navigate("< url > ");
page.evaluate("(() => {window.waitForPrintDialog = new Promise(f => window.print = f);})()");
page.getByText("Print it!").click();
page.waitForFunction("window.waitForPrintDialog");
```
```python async
await page.goto("< url > ")
await page.evaluate("(() => {window.waitForPrintDialog = new Promise(f => window.print = f);})()")
await page.get_by_text("Print it!").click()
await page.wait_for_function("window.waitForPrintDialog")
```
```python sync
page.goto("< url > ")
page.evaluate("(() => {window.waitForPrintDialog = new Promise(f => window.print = f);})()")
page.get_by_text("Print it!").click()
page.wait_for_function("window.waitForPrintDialog")
```
```csharp
await Page.GotoAsync("< url > ");
await Page.EvaluateAsync("(() => {window.waitForPrintDialog = new Promise(f => window.print = f);})()");
await Page.GetByText("Print it!").ClickAsync();
await Page.WaitForFunctionAsync("window.waitForPrintDialog");
```
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.