Implementation:Puppeteer Puppeteer Dialog
| Sources | packages/puppeteer-core/src/api/Dialog.ts |
|---|---|
| Domains | Browser Dialogs, User Interaction, Event Handling |
| Last Updated | 2026-02-12 |
Overview
Dialog is the abstract base class that represents browser dialog boxes (alert, confirm, prompt, beforeunload) dispatched by a Page via the dialog event.
Description
The Dialog class models native browser dialogs that are triggered by JavaScript calls such as alert(), confirm(), prompt(), or by the beforeunload event. Dialog instances are dispatched by the Page class through the dialog event.
Each dialog instance stores three private properties: the dialog type (from Protocol.Page.DialogType), the message text displayed in the dialog, and a default value (relevant only for prompt dialogs, empty string otherwise). These are exposed through the type(), message(), and defaultValue() accessor methods.
The class provides two user-action methods: accept() resolves the dialog affirmatively (optionally providing text input for prompt dialogs), and dismiss() rejects the dialog. Both methods include an assertion guard via the handled flag to prevent double-handling -- attempting to accept or dismiss an already-handled dialog throws an error.
The actual protocol communication for handling the dialog is delegated to the abstract handle() method, which concrete subclasses must implement.
Usage
Dialog instances are received through the page.on('dialog', ...) event listener. They must be either accepted or dismissed exactly once. If no dialog handler is registered, the browser will auto-dismiss dialogs.
Code Reference
Source Location
packages/puppeteer-core/src/api/Dialog.ts
Signature
export abstract class Dialog {
constructor(type: Protocol.Page.DialogType, message: string, defaultValue?: string);
type(): Protocol.Page.DialogType;
message(): string;
defaultValue(): string;
async accept(promptText?: string): Promise<void>;
async dismiss(): Promise<void>;
protected abstract handle(options: { accept: boolean; text?: string }): Promise<void>;
}
Import
import type {Dialog} from 'puppeteer-core/src/api/Dialog.js';
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
| type (constructor) | Protocol.Page.DialogType |
The dialog type: 'alert', 'confirm', 'prompt', or 'beforeunload' |
| message (constructor) | string |
The message displayed in the dialog |
| defaultValue (constructor) | string |
Default value for prompt dialogs (defaults to empty string) |
| promptText (accept) | undefined | Optional text to enter in a prompt dialog before accepting |
Outputs
| Method | Return Type | Description |
|---|---|---|
| type() | Protocol.Page.DialogType |
The type of the dialog |
| message() | string |
The message displayed in the dialog |
| defaultValue() | string |
The default prompt value, or empty string |
| accept() | Promise<void> |
Resolves when the dialog has been accepted |
| dismiss() | Promise<void> |
Resolves when the dialog has been dismissed |
Usage Examples
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.dismiss();
await browser.close();
});
await page.evaluate(() => alert('1'));
// Handle a confirm dialog
page.on('dialog', async dialog => {
if (dialog.type() === 'confirm') {
await dialog.accept();
}
});
const result = await page.evaluate(() => confirm('Proceed?'));
console.log(result); // true
// Handle a prompt dialog with input text
page.on('dialog', async dialog => {
if (dialog.type() === 'prompt') {
console.log('Default value:', dialog.defaultValue());
await dialog.accept('My input text');
}
});
const name = await page.evaluate(() => prompt('Enter your name:', 'Anonymous'));
console.log(name); // 'My input text'