browser_switch_to_frame
Switch to an iframe element on a webpage using locator strategies like ID, CSS, or XPath to interact with content inside embedded frames during web automation.
Instructions
Switches to an iframe element
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| by | Yes | Locator strategy to find element | |
| timeout | No | Maximum time to wait for element in milliseconds | |
| value | Yes | Value for the locator strategy |
Implementation Reference
- src/tools/elementTools.ts:294-317 (registration)Registration of the 'browser_switch_to_frame' tool with MCP server, including inline handler that delegates to ElementService.switchToFrameserver.tool( 'browser_switch_to_frame', 'Switches to an iframe element', { ...locatorSchema }, async ({ by, value, timeout = 15000 }) => { try { const driver = stateManager.getDriver(); const elementService = new ElementService(driver); await elementService.switchToFrame({ by, value, timeout }); return { content: [{ type: 'text', text: 'Switched to iframe' }], }; } catch (e) { return { content: [ { type: 'text', text: `Error switching to frame: ${(e as Error).message}`, }, ], }; } } );
- src/tools/elementTools.ts:298-316 (handler)Inline handler function for the tool that executes the frame switch logic via serviceasync ({ by, value, timeout = 15000 }) => { try { const driver = stateManager.getDriver(); const elementService = new ElementService(driver); await elementService.switchToFrame({ by, value, timeout }); return { content: [{ type: 'text', text: 'Switched to iframe' }], }; } catch (e) { return { content: [ { type: 'text', text: `Error switching to frame: ${(e as Error).message}`, }, ], }; } }
- src/services/elementService.ts:77-80 (helper)Helper method in ElementService that implements the core logic: finds the iframe element and switches the driver context to it.async switchToFrame(params: LocatorParams): Promise<void> { const element = await this.findElement(params); await this.driver.switchTo().frame(element); }
- src/types/index.ts:29-35 (schema)Zod schema for locator parameters used in the tool's input schema.export const locatorSchema = { by: z .enum(['id', 'css', 'xpath', 'name', 'tag', 'class', 'link', 'partialLink']) .describe('Locator strategy to find element'), value: z.string().describe('Value for the locator strategy'), timeout: z.number().optional().describe('Maximum time to wait for element in milliseconds'), };