browser_focus_element
Focus on a specific web element using locator strategies (id, css, xpath, etc.) with an optional timeout to wait for element availability, preparing it for subsequent interactions in browser automation.
Instructions
Focus on a specific element
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| by | Yes | Locator strategy to find element | |
| value | Yes | Value for the locator strategy | |
| timeout | No | Maximum time to wait for element in milliseconds |
Implementation Reference
- src/services/actionService.ts:97-101 (handler)The actual focus logic: locates the element using a Selenium WebDriver wait and then executes JS focus() on it.
async focusElement(params: LocatorParams): Promise<void> { const locator = LocatorFactory.createLocator(params.by, params.value); const element = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); await this.driver.executeScript('arguments[0].focus();', element); } - src/tools/actionTools.ts:438-456 (registration)Registers the 'browser_focus_element' tool on the MCP server, wiring up the schema and handler.
server.tool('browser_focus_element', 'Focus on a specific element', { ...locatorSchema }, async ({ by, value }) => { try { const driver = stateManager.getDriver(); const actionService = new ActionService(driver); await actionService.focusElement({ by, value }); return { content: [{ type: 'text', text: `Focused on element` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error focusing on element: ${(e as Error).message}`, }, ], }; } }); - src/types/index.ts:29-35 (schema)Input schema definition for element locators (by/value/timeout) used by the tool.
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'), }; - src/types/index.ts:11-15 (helper)TypeScript interface for LocatorParams used by focusElement.
export interface LocatorParams { by: LocatorStrategy; value: string; timeout?: number; }