Skip to main content
Glama

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
NameRequiredDescriptionDefault
byYesLocator strategy to find element
timeoutNoMaximum time to wait for element in milliseconds
valueYesValue for the locator strategy

Implementation Reference

  • 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}`, }, ], }; } }
  • 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); }
  • 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'), };
  • 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}`); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pshivapr/selenium-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server