Skip to main content
Glama

playwright_get_visible_text

Extracts visible text from the current web page using Playwright automation, enabling efficient content retrieval for analysis or processing.

Instructions

Get the visible text content of the current page

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The execute method of VisibleTextTool class that performs the core logic: evaluates JavaScript on the page using TreeWalker to collect visible text nodes (excluding hidden elements), trims and concatenates the text, applies truncation if necessary, and returns the result.
    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; while ((node = walker.nextNode())) { const trimmedText = node.textContent?.trim(); if (trimmedText) { text += trimmedText + "\n"; } } 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}`); } }); }
  • Tool definition including name, description, and empty inputSchema (no parameters required). Part of createToolDefinitions() array.
    name: "playwright_get_visible_text", description: "Get the visible text content of the current page", inputSchema: { type: "object", properties: {}, required: [], }, },
  • Switch case in handleToolCall that dispatches to the VisibleTextTool instance's execute method.
    case "playwright_get_visible_text": return await visibleTextTool.execute(args, context);
  • Instantiation of the VisibleTextTool class instance used for execution.
    if (!visibleTextTool) visibleTextTool = new VisibleTextTool(server);
  • Import of the VisibleTextTool class from its implementation file.
    } from './tools/browser/visiblePage.js';

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/executeautomation/mcp-playwright'

If you have feedback or need assistance with the MCP directory API, please join our Discord server