browser_wait_for
Wait for a specific element to appear on a webpage during automation, ensuring reliable interaction by pausing execution until the element is present or a timeout occurs.
Instructions
Wait for an element to appear on the page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | ||
| timeout | No |
Implementation Reference
- src/server.ts:243-269 (handler)The handler function for the 'browser_wait_for' tool. It validates input parameters using Zod, ensures the Playwright browser is connected, retrieves the current page, and waits for the specified selector to appear using page.waitForSelector with optional timeout. Returns success or error message.async (params: any) => { try { const input = z.object({ selector: z.string(), timeout: z.number().optional().default(30000) }).parse(params); await this.playwright.ensureConnected(); const page = this.playwright.getPage(); await page.waitForSelector(input.selector, { timeout: input.timeout }); return { content: [{ type: 'text', text: `Element appeared: ${input.selector}` }] }; } catch (error) { return { content: [{ type: 'text', text: `Wait for element failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
- src/server.ts:233-270 (registration)Registers the 'browser_wait_for' tool with the MCP server, including title, description, inline input schema, and the handler function.this.server.registerTool( 'browser_wait_for', { title: 'Wait for Element', description: 'Wait for an element to appear on the page', inputSchema: { selector: z.string(), timeout: z.number().optional().default(30000) } }, async (params: any) => { try { const input = z.object({ selector: z.string(), timeout: z.number().optional().default(30000) }).parse(params); await this.playwright.ensureConnected(); const page = this.playwright.getPage(); await page.waitForSelector(input.selector, { timeout: input.timeout }); return { content: [{ type: 'text', text: `Element appeared: ${input.selector}` }] }; } catch (error) { return { content: [{ type: 'text', text: `Wait for element failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- src/types.ts:36-39 (schema)Zod schema definition for the input parameters of the browser_wait_for tool: selector (required string) and timeout (optional number, default 30000). Also used for TypeScript type inference.export const BrowserWaitForInputSchema = z.object({ selector: z.string(), timeout: z.number().optional().default(30000) });