handleDialog
Control browser dialogs (alerts, confirms, prompts) by accepting, dismissing, or entering text. Simplify web automation workflows with precise dialog handling.
Instructions
Handle browser dialogs (alerts, confirms, prompts)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accept | Yes | Whether to accept (true) or dismiss (false) the dialog | |
| promptText | No | Text to enter in prompt dialogs (optional) |
Implementation Reference
- src/controllers/playwright.ts:723-743 (handler)The core handler function that sets up a one-time listener for browser dialogs (alert, confirm, prompt), logging the event, and either accepting with optional prompt text or dismissing based on the accept parameter.async handleDialog(accept: boolean, promptText?: string): Promise<void> { try { if (!this.isInitialized() || !this.state.page) { throw new Error('Browser not initialized'); } this.log('Setting up dialog handler', { accept, promptText }); this.state.page.once('dialog', async dialog => { this.log('Dialog detected', { type: dialog.type(), message: dialog.message() }); if (accept) { await dialog.accept(promptText); } else { await dialog.dismiss(); } this.log('Dialog handled'); }); } catch (error: any) { console.error('Handle dialog error:', error); throw new BrowserError('Failed to handle dialog', 'Check if there is a dialog to handle'); } }
- src/server.ts:372-389 (schema)Defines the tool schema with input validation: required 'accept' boolean and optional 'promptText' string.const HANDLE_DIALOG_TOOL: Tool = { name: "handleDialog", description: "Handle browser dialogs (alerts, confirms, prompts)", inputSchema: { type: "object", properties: { accept: { type: "boolean", description: "Whether to accept (true) or dismiss (false) the dialog" }, promptText: { type: "string", description: "Text to enter in prompt dialogs (optional)" } }, required: ["accept"] } };
- src/server.ts:543-543 (registration)Registers the handleDialog tool in the tools dictionary passed to MCP server capabilities.handleDialog: HANDLE_DIALOG_TOOL,
- src/server.ts:886-897 (registration)Dispatches the tool call to the playwrightController.handleDialog method with input validation in the MCP callTool request handler.case 'handleDialog': { if (typeof args.accept !== 'boolean') { return { content: [{ type: "text", text: "Accept parameter is required" }], isError: true }; } await playwrightController.handleDialog(args.accept, args.promptText as string); return { content: [{ type: "text", text: "Dialog handler set successfully" }] }; }