browser_switch_to_window_by_url
Switch browser focus to a specific window by matching its URL, enabling control of multiple tabs during automated web testing.
Instructions
Switch to a window by its URL
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | The URL of the window to switch to |
Implementation Reference
- src/tools/browserTools.ts:370-396 (handler)Executes the tool logic: retrieves the browser driver, iterates over all window handles, switches to each window, compares the current URL with the target URL, and switches to the matching window or reports not found/error.async ({ url }) => { try { const driver = stateManager.getDriver(); const windowHandles = await driver.getAllWindowHandles(); for (const handle of windowHandles) { await driver.switchTo().window(handle); const currentUrl = await driver.getCurrentUrl(); if (currentUrl === url) { return { content: [{ type: 'text', text: `Switched to window: ${url}` }], }; } } return { content: [{ type: 'text', text: `Window with URL '${url}' not found` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error switching to window by URL: ${(e as Error).message}`, }, ], }; } }
- src/tools/browserTools.ts:364-397 (registration)Registers the 'browser_switch_to_window_by_url' tool with the MCP server inside registerBrowserTools function, specifying name, description, input schema {url: z.string()}, and handler.server.tool( 'browser_switch_to_window_by_url', 'Switch to a window by its URL', { url: z.string().describe('The URL of the window to switch to'), }, async ({ url }) => { try { const driver = stateManager.getDriver(); const windowHandles = await driver.getAllWindowHandles(); for (const handle of windowHandles) { await driver.switchTo().window(handle); const currentUrl = await driver.getCurrentUrl(); if (currentUrl === url) { return { content: [{ type: 'text', text: `Switched to window: ${url}` }], }; } } return { content: [{ type: 'text', text: `Window with URL '${url}' not found` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error switching to window by URL: ${(e as Error).message}`, }, ], }; } } );
- src/tools/index.ts:9-9 (registration)Top-level registration call to registerBrowserTools from registerAllTools, which includes the browser_switch_to_window_by_url tool.registerBrowserTools(server, stateManager);
- src/tools/browserTools.ts:368-369 (schema)Zod input schema for the tool: url as a string with description.url: z.string().describe('The URL of the window to switch to'), },