browser_find_elements
Locate multiple web elements using various strategies like CSS, XPath, or ID to automate browser interactions and testing workflows.
Instructions
Find multiple elements
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:33-59 (registration)Registers the browser_find_elements MCP tool, defining its name, description, input schema, and handler function that retrieves the WebDriver, instantiates ElementService, calls findElements, and returns success/error messages.server.tool( 'browser_find_elements', 'Find multiple elements', { ...locatorSchema }, async ({ by, value, timeout = 15000 }) => { try { const driver = stateManager.getDriver(); const elementService = new ElementService(driver); const elements = await elementService.findElements({ by, value, timeout, }); return { content: [{ type: 'text', text: `Found ${elements.length} elements` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error finding elements: ${(e as Error).message}`, }, ], }; } }
- src/services/elementService.ts:13-17 (handler)ElementService.findElements: core logic to create locator, wait for multiple elements to be located using selenium-webdriver until.elementsLocated, then find and return the WebElements.async findElements(params: LocatorParams): Promise<WebElement[]> { const locator = LocatorFactory.createLocator(params.by, params.value); await this.driver.wait(until.elementsLocated(locator), params.timeout || 15000); return this.driver.findElements(locator); }
- src/types/index.ts:29-35 (schema)Zod schema definition for locator parameters (by, value, timeout) used in the tool's input validation.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/utils/locators.ts:5-26 (helper)LocatorFactory.createLocator: utility to convert locator strategy and value into appropriate selenium-webdriver By locator instance.static createLocator(by: LocatorStrategy, value: string): Locator { switch (by.toLowerCase()) { case 'id': return By.id(value); case 'css': return By.css(value); case 'xpath': return By.xpath(value); case 'name': return By.name(value); case 'tag': return By.css(value); case 'class': return By.className(value); case 'link': return By.linkText(value); case 'partialLink': return By.partialLinkText(value); default: throw new Error(`Unsupported locator strategy: ${by}`); } }