browser_find_element
Locate web page elements using various strategies like ID, CSS, XPath, or class name to enable automated browser interaction and testing.
Instructions
Find an 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:9-31 (handler)MCP tool handler implementation for 'browser_find_element'. It retrieves the WebDriver from state, instantiates ElementService, calls findElement, and returns a textual response indicating success or error.'browser_find_element', 'Find an element', { ...locatorSchema }, async ({ by, value, timeout = 15000 }) => { try { const driver = stateManager.getDriver(); const elementService = new ElementService(driver); await elementService.findElement({ by, value, timeout }); return { content: [{ type: 'text', text: 'Element found' }], }; } catch (e) { return { content: [ { type: 'text', text: `Error finding element: ${(e as Error).message}`, }, ], }; } } );
- src/types/index.ts:29-35 (schema)Zod schema defining input parameters for element locator tools, used by browser_find_element.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/services/elementService.ts:8-11 (helper)Core helper method in ElementService that creates a locator using LocatorFactory and waits for the element using Selenium's WebDriver.async findElement(params: LocatorParams): Promise<WebElement> { const locator = LocatorFactory.createLocator(params.by, params.value); return this.driver.wait(until.elementLocated(locator), params.timeout || 15000); }
- src/tools/index.ts:8-13 (registration)Top-level tool registration function that calls registerElementTools, thereby registering the browser_find_element tool.export function registerAllTools(server: McpServer, stateManager: StateManager): void { registerBrowserTools(server, stateManager); registerElementTools(server, stateManager); registerActionTools(server, stateManager); registerCookieTools(server, stateManager); }