Skip to main content
Glama

playwright_get_text_content

Retrieve text content from all elements on a webpage using the Playwright Server for browser automation tasks.

Instructions

Get the text content of all elements

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • GetTextContentToolHandler class: executes JavaScript to collect unique text content from visible elements with few children (≤3), up to 1000 chars, including values, from the current browser page.
    class GetTextContentToolHandler(ToolHandler): async def handle(self, name: str, arguments: dict | None) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: if not self._sessions: return [types.TextContent(type="text", text="No active session. Please create a new session first.")] session_id = list(self._sessions.keys())[-1] page = self._sessions[session_id]["page"] # text_contents = await page.locator('body').all_inner_texts() async def get_unique_texts_js(page): unique_texts = await page.evaluate('''() => { var elements = Array.from(document.querySelectorAll('*')); // 先选择所有元素,再进行过滤 var uniqueTexts = new Set(); for (var element of elements) { if (element.offsetWidth > 0 || element.offsetHeight > 0) { // 判断是否可见 var childrenCount = element.querySelectorAll('*').length; if (childrenCount <= 3) { var innerText = element.innerText ? element.innerText.trim() : ''; if (innerText && innerText.length <= 1000) { uniqueTexts.add(innerText); } var value = element.getAttribute('value'); if (value) { uniqueTexts.add(value); } } } } //console.log( Array.from(uniqueTexts)); return Array.from(uniqueTexts); } ''') return unique_texts # 使用示例 text_contents = await get_unique_texts_js(page) return [types.TextContent(type="text", text=f"Text content of all elements: {text_contents}")]
  • Tool schema definition in list_tools(): no input parameters required.
    types.Tool( name="playwright_get_text_content", description="Get the text content of all elements", inputSchema={ "type": "object", "properties": { }, } ),
  • Registration of tool handlers in the tool_handlers dictionary, mapping tool name to its handler instance.
    tool_handlers = { "playwright_navigate": NavigateToolHandler(), "playwright_screenshot": ScreenshotToolHandler(), "playwright_click": ClickToolHandler(), "playwright_fill": FillToolHandler(), "playwright_evaluate": EvaluateToolHandler(), "playwright_click_text": ClickTextToolHandler(), "playwright_get_text_content": GetTextContentToolHandler(), "playwright_get_html_content": GetHtmlContentToolHandler(), "playwright_new_session":NewSessionToolHandler(), }

Other Tools

Related Tools

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/williamvd4/playwright-plus-python-mcp'

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