browser_get_attribute
Retrieve attribute values from web elements using locator strategies like ID, CSS, or XPath for automated web testing and data extraction.
Instructions
Gets the value of an attribute from an element
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| attribute | Yes | Name of the attribute to get | |
| 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:169-205 (registration)Registers the 'browser_get_attribute' MCP tool with server.tool(), including inline input schema (extending locatorSchema with attribute) and handler function that instantiates 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 (helper)Core helper method in ElementService that locates the element using findElement and retrieves the specified attribute value via Selenium WebDriver's 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)locatorSchema: Zod object defining base input parameters (by: locator strategy, value: string, optional timeout) extended by the tool 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'), };