browser_get_attribute
Extract attribute values from web elements using locator strategies like ID, CSS, or XPath for automated web testing and data retrieval.
Instructions
Gets the value of an attribute from an element
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| by | Yes | Locator strategy to find element | |
| value | Yes | Value for the locator strategy | |
| timeout | No | Maximum time to wait for element in milliseconds | |
| attribute | Yes | Name of the attribute to get |
Implementation Reference
- src/tools/elementTools.ts:169-205 (registration)Registers the browser_get_attribute tool with MCP server.tool(), including input schema (locatorSchema + attribute field) and the handler function that creates ElementService and calls getElementAttribute.server.tool( 'browser_get_attribute', 'Gets the value of an attribute from an element', { ...locatorSchema, attribute: z.string().describe('Name of the attribute to get'), }, async ({ by, value, attribute, timeout = 15000 }) => { try { const driver = stateManager.getDriver(); const elementService = new ElementService(driver); const attrValue = await elementService.getElementAttribute({ by, value, attribute, timeout, }); return { content: [ { type: 'text', text: `Attribute "${attribute}" has value: ${attrValue}`, }, ], }; } catch (e) { return { content: [ { type: 'text', text: `Error getting attribute: ${(e as Error).message}`, }, ], }; } } );
- src/services/elementService.ts:35-38 (handler)Core handler logic in ElementService class: locates the element using findElement and retrieves the specified attribute value using Selenium WebElement.getAttribute()async getElementAttribute(params: LocatorParams & { attribute: string }): Promise<string | null> { const element = await this.findElement(params); return element.getAttribute(params.attribute); }
- src/types/index.ts:29-35 (schema)Zod schema object for common locator parameters (by, value, timeout), spread into the tool's input schema.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'), };