Skip to main content
Glama

browser_switch_window_by_index

Switch between multiple browser windows or tabs using numerical indexing to manage web automation tasks across different pages.

Instructions

Switch to a window by its index

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
indexYesThe index of the window to switch to

Implementation Reference

  • Handler function that switches the browser window by the given index using Selenium WebDriver. Validates index range, retrieves window handles, switches to the specified handle, and returns success or error message.
    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}`, }, ], }; } }
  • Zod schema defining the input parameter 'index' as a number for the tool.
    { index: z.number().describe('The index of the window to switch to'), },
  • Registers the 'browser_switch_window_by_index' tool on the MCP server with name, description, input schema, and inline handler function.
    '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}`, }, ], }; } } );

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