browser_switch_window_by_index
Switch between browser windows by specifying their index number to manage multiple tabs or windows during web automation tasks.
Instructions
Switch to a window by its index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | Yes | The index of the window to switch to |
Implementation Reference
- src/tools/browserTools.ts:327-362 (registration)Registration of the 'browser_switch_window_by_index' tool, including input schema (index: number) and the inline handler function that uses Selenium WebDriver to get all window handles, validate the index, and switch to the specified window.'browser_switch_window_by_index', 'Switch to a window by its index', { index: z.number().describe('The index of the window to switch to'), }, async ({ index }) => { try { const driver = stateManager.getDriver(); const windowHandles = await driver.getAllWindowHandles(); if (index < 0 || index >= windowHandles.length) { return { content: [{ type: 'text', text: `Invalid window index: ${index}` }], }; } const handle = windowHandles[index]; if (!handle) { return { content: [{ type: 'text', text: `No window handle found at index: ${index}` }], }; } await driver.switchTo().window(handle); return { content: [{ type: 'text', text: `Switched to window at index: ${index}` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error switching to window by index: ${(e as Error).message}`, }, ], }; } } );
- src/tools/browserTools.ts:332-361 (handler)The core handler logic for the tool: retrieves the current WebDriver instance, fetches all window handles, checks if the index is valid, switches to the window at that index, and returns appropriate success or error messages.async ({ index }) => { try { const driver = stateManager.getDriver(); const windowHandles = await driver.getAllWindowHandles(); if (index < 0 || index >= windowHandles.length) { return { content: [{ type: 'text', text: `Invalid window index: ${index}` }], }; } const handle = windowHandles[index]; if (!handle) { return { content: [{ type: 'text', text: `No window handle found at index: ${index}` }], }; } await driver.switchTo().window(handle); return { content: [{ type: 'text', text: `Switched to window at index: ${index}` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error switching to window by index: ${(e as Error).message}`, }, ], }; } }
- src/tools/browserTools.ts:330-331 (schema)Input schema for the tool using Zod: requires an 'index' parameter as a number.index: z.number().describe('The index of the window to switch to'), },