Skip to main content
Glama

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
NameRequiredDescriptionDefault
indexYesThe index of the window to switch to

Implementation Reference

  • 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}`, }, ], }; } } );
  • 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}`, }, ], }; } }
  • 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'), },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pshivapr/selenium-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server