switch_window
Switches the active browser context to a specific window or tab using its Selenium window handle. Useful for managing multiple browser windows or tabs during automation.
Instructions
Switch to a browser window or tab by Selenium window handle.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| handle | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The core handler: BrowserManager.switch_window() calls Selenium's switch_to.window(handle) with thread safety.
def switch_window(self, handle: str) -> BrowserState: with self._lock: self._require_driver().switch_to.window(handle) return self.state() - src/selenium_mcp_server/server.py:95-98 (registration)The MCP tool registration: switch_window is decorated with @mcp.tool() and delegates to browser.switch_window via _run().
@mcp.tool() def switch_window(handle: str) -> dict[str, Any]: """Switch to a browser window or tab by Selenium window handle.""" return _run("switch_window", browser.switch_window, handle) - The _run() helper that executes any browser method and converts the result via _as_dict(), with error handling.
def _run(action: str, func: Any, *args: Any, **kwargs: Any) -> Any: try: return _as_dict(func(*args, **kwargs)) except BrowserError: logger.exception("Browser action failed: %s", action) raise except Exception as exc: logger.exception("Unexpected Selenium MCP error during %s", action) raise BrowserError(f"{action} failed: {exc}") from exc - The _as_dict() helper that serializes Pydantic models (like BrowserState) to dictionaries.
def _as_dict(value: Any) -> Any: if hasattr(value, "model_dump"): return value.model_dump() return value - The BrowserState model returned by switch_window, containing session_id, URL, title, window_handles, and active_window_handle.
class BrowserState(BaseModel): session_id: str | None current_url: str | None title: str | None window_handles: list[str] active_window_handle: str | None