Skip to main content
Glama

browser_get_element_text

Extract text content from web page elements using Selenium WebDriver. Specify locator strategy and value to retrieve text from specific elements during browser automation.

Instructions

Gets the text of an element

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
byYesLocator strategy to find element
valueYesValue for the locator strategy
timeoutNoMaximum time to wait for element in milliseconds

Implementation Reference

  • The MCP tool handler function registered for 'browser_get_element_text'. It retrieves the browser driver, instantiates ElementService, fetches the element text, and returns it in the MCP response format, with error handling.
    server.tool( 'browser_get_element_text', 'Gets the text of an element', { ...locatorSchema }, async ({ by, value, timeout = 15000 }) => { try { const driver = stateManager.getDriver(); const elementService = new ElementService(driver); const text = await elementService.getElementText({ by, value, timeout, }); return { content: [{ type: 'text', text }], }; } catch (e) { return { content: [ { type: 'text', text: `Error getting element text: ${(e as Error).message}`, }, ], }; } } );
  • Zod-based input schema for the tool parameters (locator strategy, value, optional timeout).
    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'), };
  • The ElementService method implementing the core logic: finds the element using the locator and retrieves its text using Selenium WebDriver's getText().
    async getElementText(params: LocatorParams): Promise<string> { const element = await this.findElement(params); return element.getText(); }
  • Call to register the element tools, including 'browser_get_element_text', within the overall tools registration.
    registerElementTools(server, stateManager);
  • Supporting findElement method used by getElementText to locate the WebElement.
    async findElement(params: LocatorParams): Promise<WebElement> { const locator = LocatorFactory.createLocator(params.by, params.value); return this.driver.wait(until.elementLocated(locator), params.timeout || 15000); }

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