wait_for_selector
Wait for a web page element to become visible before proceeding with automation tasks, ensuring reliable interaction with dynamic content.
Instructions
Wait for an element to be visible on the page
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | ||
| page_id | No | ||
| timeout | No |
Implementation Reference
- src/playwright_mcp/server.py:324-337 (handler)Handler implementation for the 'wait_for_selector' tool. Extracts selector and optional timeout/page_id from arguments, retrieves the page, and calls page.wait_for_selector with error handling.
elif name == "wait_for_selector": selector = arguments.get("selector") if not selector: raise ValueError("Selector is required") timeout = arguments.get("timeout", 30000) # Default 30 seconds page = get_active_page(arguments.get("page_id")) try: await page.wait_for_selector(selector, timeout=timeout) return [types.TextContent(type="text", text=f"Element found: {selector}")] except Exception as e: return [types.TextContent(type="text", text=f"Timeout waiting for element: {selector}")] - src/playwright_mcp/server.py:185-197 (registration)Registration of the 'wait_for_selector' tool in the list_tools handler, including name, description, and input schema.
types.Tool( name="wait_for_selector", description="Wait for an element to be visible on the page", inputSchema={ "type": "object", "properties": { "selector": {"type": "string"}, "page_id": {"type": "string"}, "timeout": {"type": "number"}, }, "required": ["selector"], }, ), - src/playwright_mcp/server.py:200-210 (helper)Helper function used by the handler to retrieve the active browser page based on page_id or default.
def get_active_page(page_id: Optional[str] = None) -> Page: """Get the active page based on page_id or current default.""" global current_page_id if page_id is None: page_id = current_page_id if page_id not in pages: raise ValueError(f"Page not found: {page_id}") return pages[page_id]