browser_find_element
Locate web page elements using Selenium WebDriver for browser automation and testing. Specify locator strategies like ID, CSS, or XPath to find elements within a configurable timeout.
Instructions
Find an element
Input Schema
TableJSON 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/tools/elementTools.ts:12-30 (handler)The inline handler function for the browser_find_element tool. It retrieves the WebDriver, instantiates ElementService, calls findElement, and returns a success or error message.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/tools/elementTools.ts:8-31 (registration)The registration of the browser_find_element tool using server.tool(), including name, description, input schema, and handler function.server.tool( '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)The locatorSchema defining the input parameters (by, value, timeout) for element location, used in the tool 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'), };
- src/services/elementService.ts:8-11 (helper)The findElement helper method in ElementService that creates a locator and waits for the element using Selenium 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)Higher-level registration function that calls registerElementTools, which registers the browser_find_element tool among others.export function registerAllTools(server: McpServer, stateManager: StateManager): void { registerBrowserTools(server, stateManager); registerElementTools(server, stateManager); registerActionTools(server, stateManager); registerCookieTools(server, stateManager); }