Implementation:Microsoft Playwright Client Dialog
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, User Interface |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for representing and handling browser dialog boxes (alert, confirm, prompt, beforeunload) provided by the Playwright library.
Description
The Dialog class extends ChannelOwner and implements the api.Dialog interface. It represents a JavaScript dialog (alert, confirm, prompt, or beforeunload) that appears in a browser page. The constructor resolves the associated Page from the initializer (which may be null for dialogs that open early during page initialization). The class provides read-only accessors for the dialog type(), message(), and defaultValue() (for prompts), plus action methods accept() (with optional prompt text) and dismiss() that communicate with the server to handle the dialog.
Usage
Use the Dialog class to handle JavaScript dialogs that appear during page interactions. Obtain instances by listening to the 'dialog' event on a Page or BrowserContext. Dialogs must be handled (accepted or dismissed) to allow page execution to continue.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File:
packages/playwright-core/src/client/dialog.ts
Signature
export class Dialog extends ChannelOwner<channels.DialogChannel> implements api.Dialog {
static from(dialog: channels.DialogChannel): Dialog;
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.DialogInitializer);
page(): Page | null;
type(): string;
message(): string;
defaultValue(): string;
async accept(promptText?: string): Promise<void>;
async dismiss(): Promise<void>;
}
Import
import { Dialog } from 'playwright-core/src/client/dialog';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| promptText (accept) | string |
No | Text to enter in a prompt dialog before accepting |
Outputs
| Name | Type | Description |
|---|---|---|
| page() | null | The page that generated the dialog, or null if opened during early initialization |
| type() | string |
The dialog type: 'alert', 'confirm', 'prompt', or 'beforeunload' |
| message() | string |
The message displayed in the dialog |
| defaultValue() | string |
The default value of a prompt dialog |
Usage Examples
page.on('dialog', async (dialog) => {
console.log(`Dialog type: ${dialog.type()}`);
console.log(`Message: ${dialog.message()}`);
if (dialog.type() === 'prompt') {
await dialog.accept('my input');
} else if (dialog.type() === 'confirm') {
await dialog.accept();
} else {
await dialog.dismiss();
}
});
// Trigger a dialog
await page.evaluate(() => alert('Hello!'));