browser_wait_for
Use this tool to pause execution until a specified web element appears on a page, ensuring interactions occur only when the element is present. Ideal for web automation tasks requiring precise timing and element availability.
Instructions
Wait for an element to appear on the page
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | ||
| timeout | No |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"selector": {
"type": "string"
},
"timeout": {
"default": 30000,
"type": "number"
}
},
"required": [
"selector"
],
"type": "object"
}
Implementation Reference
- src/server.ts:243-269 (handler)The handler function executes the browser_wait_for tool logic: validates input, connects to Playwright if needed, retrieves the current page, waits for the specified selector with optional timeout using page.waitForSelector, and returns appropriate success or error response.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)The registration of the 'browser_wait_for' tool in the PlaywrightMcpServer.setupTools() method, including inline input schema definition, tool metadata, and the inline 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 browser_wait_for tool input (matches the inline schema used in registration).export const BrowserWaitForInputSchema = z.object({ selector: z.string(), timeout: z.number().optional().default(30000) });
- src/types.ts:64-64 (schema)TypeScript type definition inferred from the BrowserWaitForInputSchema.export type BrowserWaitForInput = z.infer<typeof BrowserWaitForInputSchema>;