waitForSelector
Pause execution until a specified HTML element appears on the webpage, ensuring synchronization in browser automation tasks. Input includes a CSS selector and optional timeout.
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)Core handler function implementing the waitForSelector tool logic using Playwright's page.waitForSelector method with logging and error handling.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:860-871 (handler)Dispatch handler in the MCP callTool request handler that validates input and delegates to the Playwright controller'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" }] }; }
- src/server.ts:343-357 (schema)Tool schema definition including input schema with required 'selector' parameter 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:555-565 (registration)Server initialization registering all tools (including waitForSelector via the 'tools' object) in MCP capabilities.const server = new Server( { name: "playmcp-browser", version: "1.0.0", }, { capabilities: { tools, }, } );
- src/server.ts:541-541 (registration)Specific inclusion of the waitForSelector tool in the tools dictionary used for MCP registration.waitForSelector: WAIT_FOR_SELECTOR_TOOL,