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
| 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:144-166 (handler)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}`, }, ], }; } }
- src/types/index.ts:29-35 (schema)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'), };
- src/tools/elementTools.ts:140-167 (registration)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}`, }, ], }; } } );
- src/services/elementService.ts:19-22 (helper)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(); }