waitForSelector
Waits for a specific HTML element to appear on a webpage before proceeding, ensuring reliable automation by synchronizing with page loading and dynamic content.
Instructions
Wait for a specific selector to appear on the page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | ||
| timeout | No | Timeout in milliseconds (default: 30000) |
Implementation Reference
- src/controllers/playwright.ts:694-706 (handler)The core handler function implementing waitForSelector using Playwright's page.waitForSelector method, with error handling and logging.async waitForSelector(selector: string, timeout: number = 30000): Promise<void> { try { if (!this.isInitialized() || !this.state.page) { throw new Error('Browser not initialized'); } this.log('Waiting for selector', { selector, timeout }); await this.state.page.waitForSelector(selector, { timeout }); this.log('Selector found'); } catch (error: any) { console.error('Wait for selector error:', error); throw new BrowserError('Selector not found within timeout', 'Check if the selector appears on the page'); } }
- src/server.ts:343-357 (schema)Input schema definition for the waitForSelector tool, specifying selector (required) and optional timeout.const WAIT_FOR_SELECTOR_TOOL: Tool = { name: "waitForSelector", description: "Wait for a specific selector to appear on the page", inputSchema: { type: "object", properties: { selector: { type: "string" }, timeout: { type: "number", description: "Timeout in milliseconds (default: 30000)" } }, required: ["selector"] } };
- src/server.ts:541-541 (registration)Registration of the waitForSelector tool in the tools object passed to MCP server capabilities.waitForSelector: WAIT_FOR_SELECTOR_TOOL,
- src/server.ts:860-871 (registration)Dispatch handler in the callTool request handler switch statement that invokes the playwrightController's waitForSelector method.case 'waitForSelector': { if (!args.selector) { return { content: [{ type: "text", text: "Selector is required" }], isError: true }; } await playwrightController.waitForSelector(args.selector as string, args.timeout as number); return { content: [{ type: "text", text: "Selector found successfully" }] }; }