get_element_text
Retrieve the visible text content of a web page element using its CSS selector. Specify a timeout for waiting if the element is not immediately present.
Instructions
Get text content of an element
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | CSS selector for the element | |
| timeout | No | Timeout in milliseconds |
Implementation Reference
- src/index.ts:44-47 (schema)Zod schema for get_element_text input validation, defining 'selector' (string, required) and 'timeout' (number, default 5000ms).
const GetElementTextSchema = z.object({ selector: z.string(), timeout: z.number().default(5000) }); - src/index.ts:237-255 (registration)Registration of the 'get_element_text' tool in the ListTools handler, specifying name, description, and inputSchema.
{ name: 'get_element_text', description: 'Get text content of an element', inputSchema: { type: 'object', properties: { selector: { type: 'string', description: 'CSS selector for the element' }, timeout: { type: 'number', default: 5000, description: 'Timeout in milliseconds' } }, required: ['selector'] } }, - src/index.ts:544-560 (handler)Handler case for 'get_element_text': validates input via GetElementTextSchema, calls currentPage.textContent() with the selector and timeout, and returns the element's text content.
case 'get_element_text': { if (!currentPage) { throw new Error('No browser page available. Launch a browser first.'); } const params = GetElementTextSchema.parse(args); const text = await currentPage.textContent(params.selector, { timeout: params.timeout }); return { content: [ { type: 'text', text: `Element text: ${text || '(empty)'}` } ] }; }