playwright_get_visible_text
Extract visible text content from web pages to retrieve human-readable information for data collection, analysis, or testing purposes.
Instructions
Get the visible text content of the current page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/browser/visiblePage.ts:8-63 (handler)VisibleTextTool class that implements the core logic for the 'playwright_get_visible_text' tool. It uses Playwright's page.evaluate to run JavaScript that traverses the DOM with TreeWalker, collecting visible text nodes (excluding hidden elements), concatenates them, applies truncation if needed, and returns the result.export class VisibleTextTool extends BrowserToolBase { /** * Execute the visible text page tool */ async execute(args: any, context: ToolContext): Promise<ToolResponse> { // Check if browser is available if (!context.browser || !context.browser.isConnected()) { // If browser is not connected, we need to reset the state to force recreation resetBrowserState(); return createErrorResponse( "Browser is not connected. The connection has been reset - please retry your navigation.", ); } // Check if page is available and not closed if (!context.page || context.page.isClosed()) { return createErrorResponse("Page is not available or has been closed. Please retry your navigation."); } return this.safeExecute(context, async (page) => { try { const visibleText = await page?.evaluate(() => { const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, { acceptNode: (node) => { const style = window.getComputedStyle(node.parentElement!); return style.display !== "none" && style.visibility !== "hidden" ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT; }, }); let text = ""; let node: Node | null = null; node = walker.nextNode(); while (node) { const trimmedText = node.textContent?.trim(); if (trimmedText) { text += `${trimmedText}\n`; } node = walker.nextNode(); } return text.trim(); }); // Truncate logic const maxLength = typeof args.maxLength === "number" ? args.maxLength : 20000; let output = visibleText; let _truncated = false; if (output.length > maxLength) { output = `${output.slice(0, maxLength)}\n[Output truncated due to size limits]`; _truncated = true; } return createSuccessResponse(`Visible text content:\n${output}`); } catch (error) { return createErrorResponse(`Failed to get visible text content: ${(error as Error).message}`); } }); } }
- src/tools.ts:384-392 (schema)Input schema definition for the tool, specifying no required parameters.{ name: "playwright_get_visible_text", description: "Get the visible text content of the current page", inputSchema: { type: "object", properties: {}, required: [], }, },
- src/toolHandler.ts:618-619 (registration)Registration and dispatch in the main tool handler switch statement, calling the VisibleTextTool's execute method.case "playwright_get_visible_text": return await visibleTextTool.execute(args, context);
- src/toolHandler.ts:403-403 (registration)Instantiation of the VisibleTextTool instance used by the handler.if (!visibleTextTool) visibleTextTool = new VisibleTextTool(server);
- src/toolHandler.ts:35-35 (registration)Import of the VisibleTextTool class.import { VisibleHtmlTool, VisibleTextTool } from "./tools/browser/visiblePage.js";