browser_get_element_text
Extract text content from a specified HTML element using a selector and instance ID within a concurrent browser environment. Supports custom timeouts for reliable results.
Instructions
Get element text content
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| instanceId | Yes | Instance ID | |
| selector | Yes | Element selector | |
| timeout | No | Timeout in milliseconds |
Implementation Reference
- src/tools.ts:846-866 (handler)Core implementation of the browser_get_element_text tool. Retrieves the text content of the specified element using Playwright's page.textContent method, with instance validation and error handling.private async getElementText(instanceId: string, selector: string, timeout: number): Promise<ToolResult> { const instance = this.browserManager.getInstance(instanceId); if (!instance) { return { success: false, error: `Instance ${instanceId} not found` }; } try { const text = await instance.page.textContent(selector, { timeout }); return { success: true, data: { selector, text }, instanceId }; } catch (error) { return { success: false, error: `Get element text failed: ${error instanceof Error ? error.message : error}`, instanceId }; } }
- src/tools.ts:307-328 (schema)Input schema definition for the browser_get_element_text tool, specifying parameters like instanceId, selector, and optional timeout.{ name: 'browser_get_element_text', description: 'Get element text content', inputSchema: { type: 'object', properties: { instanceId: { type: 'string', description: 'Instance ID' }, selector: { type: 'string', description: 'Element selector', }, timeout: { type: 'number', description: 'Timeout in milliseconds', default: 30000 } }, required: ['instanceId', 'selector'] }
- src/tools.ts:556-557 (registration)Dispatch registration in the executeTools switch statement, mapping the tool name to its handler function.case 'browser_get_element_text': return await this.getElementText(args.instanceId, args.selector, args.timeout || 30000);
- src/server.ts:40-45 (registration)MCP server handler for listing tools, which includes browser_get_element_text via BrowserTools.getTools().this.server.setRequestHandler(ListToolsRequestSchema, async () => { const tools = this.browserTools.getTools(); return { tools: tools, }; });
- src/server.ts:48-75 (registration)MCP server handler for calling tools, delegating to BrowserTools.executeTools which handles browser_get_element_text.this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { const result = await this.browserTools.executeTools(name, args || {}); if (result.success) { return { content: [ { type: 'text', text: JSON.stringify(result.data, null, 2), }, ], }; } else { throw new McpError(ErrorCode.InternalError, result.error || 'Tool execution failed'); } } catch (error) { if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Tool execution failed: ${error instanceof Error ? error.message : error}` ); } });