switch_page
Navigate between browser pages in Playwright automation by specifying a page ID to switch context for testing or scraping tasks.
Instructions
Switch to a different browser page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| page_id | Yes |
Implementation Reference
- src/playwright_mcp/server.py:166-176 (registration)Registration of the 'switch_page' tool in the list_tools handler, including its schema definition.types.Tool( name="switch_page", description="Switch to a different browser page", inputSchema={ "type": "object", "properties": { "page_id": {"type": "string"}, }, "required": ["page_id"], }, ),
- src/playwright_mcp/server.py:305-315 (handler)Handler logic for the 'switch_page' tool: retrieves page_id from arguments, validates existence, updates global current_page_id, and returns confirmation.elif name == "switch_page": page_id = arguments.get("page_id") if not page_id: raise ValueError("Page ID is required") if page_id not in pages: raise ValueError(f"Page ID '{page_id}' not found") current_page_id = page_id return [types.TextContent(type="text", text=f"Switched to page: {page_id}")]
- src/playwright_mcp/server.py:200-210 (helper)Helper function get_active_page used by switch_page and other tools to retrieve the active Page object based on page_id or current_page_id.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]