get_element_text
Extract text content from a web element using a CSS selector with the MCP Browser Server, enabling automated data retrieval from web pages.
Instructions
Get text content of an element
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | CSS selector for the element | |
| timeout | No | Timeout in milliseconds |
Implementation Reference
- src/index.ts:544-560 (handler)Handler for 'get_element_text' tool: checks for current page, parses input parameters using GetElementTextSchema, retrieves the text content of the specified element using Playwright's page.textContent method with timeout, and returns the extracted text.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)'}` } ] }; }
- src/index.ts:44-47 (schema)Zod schema defining the input parameters for the 'get_element_text' tool: CSS selector (required) and optional timeout (default 5000ms).const GetElementTextSchema = z.object({ selector: z.string(), timeout: z.number().default(5000) });
- src/index.ts:237-255 (registration)Tool registration in the ListToolsRequestSchema handler, providing name, description, and JSON schema matching the Zod schema for client-side validation.{ 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'] } },