Skip to main content
Glama

browser_get_element_text

Extract text content from web elements using locator strategies like ID, CSS, or XPath to retrieve displayed text for automation and testing purposes.

Instructions

Gets the text of an element

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

  • The inline handler function passed to server.tool for executing browser_get_element_text. It retrieves the WebDriver, instantiates ElementService, calls getElementText on the specified locator, and returns the text or error message.
    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 schema defining the input parameters (locator strategy, value, optional timeout) for the browser_get_element_text tool.
    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 server.tool() call that registers the browser_get_element_text tool with the MCP server, specifying name, description, input schema, and handler.
    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}`, }, ], }; } } );
  • ElementService method implementing the core logic: locates the element via findElement and retrieves its text using Selenium WebElement.getText().
    async getElementText(params: LocatorParams): Promise<string> { const element = await this.findElement(params); return element.getText(); }

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