playwright_get_text_content
Extract text content from web page elements for data collection or content analysis during browser automation.
Instructions
Get the text content of all elements
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/playwright_server/server.py:281-322 (handler)The GetTextContentToolHandler class implements the core logic for the playwright_get_text_content tool. It retrieves the current browser page session, executes custom JavaScript to collect unique text contents from visible elements with few children (≤3), and returns them as text content.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}")]
- JSON Schema definition for the tool in the list_tools response, defining the tool name, description, and an empty input schema (no required parameters).types.Tool( name="playwright_get_text_content", description="Get the text content of all elements", inputSchema={ "type": "object", "properties": { }, } ),
- src/playwright_server/server.py:341-341 (registration)The tool is registered in the tool_handlers dictionary, mapping the tool name to an instance of GetTextContentToolHandler for execution in the call_tool handler."playwright_get_text_content": GetTextContentToolHandler(),
- src/playwright_server/server.py:47-152 (registration)The tool is registered in the list_tools handler by including it in the returned list of available tools with its schema.@server.list_tools() async def handle_list_tools() -> list[types.Tool]: """ List available tools. Each tool specifies its arguments using JSON Schema validation. """ return [ # types.Tool( # name="playwright_new_session", # description="Create a new browser session", # inputSchema={ # "type": "object", # "properties": { # "url": {"type": "string", "description": "Initial URL to navigate to"} # } # } # ), types.Tool( name="playwright_navigate", description="Navigate to a URL,thip op will auto create a session", inputSchema={ "type": "object", "properties": { "url": {"type": "string"} }, "required": ["url"] } ), types.Tool( name="playwright_screenshot", description="Take a screenshot of the current page or a specific element", inputSchema={ "type": "object", "properties": { "name": {"type": "string"}, "selector": {"type": "string", "description": "CSS selector for element to screenshot,null is full page"}, }, "required": ["name"] } ), types.Tool( name="playwright_click", description="Click an element on the page using CSS selector", inputSchema={ "type": "object", "properties": { "selector": {"type": "string", "description": "CSS selector for element to click"} }, "required": ["selector"] } ), types.Tool( name="playwright_fill", description="Fill out an input field", inputSchema={ "type": "object", "properties": { "selector": {"type": "string", "description": "CSS selector for input field"}, "value": {"type": "string", "description": "Value to fill"} }, "required": ["selector", "value"] } ), types.Tool( name="playwright_evaluate", description="Execute JavaScript in the browser console", inputSchema={ "type": "object", "properties": { "script": {"type": "string", "description": "JavaScript code to execute"} }, "required": ["script"] } ), types.Tool( name="playwright_click_text", description="Click an element on the page by its text content", inputSchema={ "type": "object", "properties": { "text": {"type": "string", "description": "Text content of the element to click"} }, "required": ["text"] } ), types.Tool( name="playwright_get_text_content", description="Get the text content of all elements", inputSchema={ "type": "object", "properties": { }, } ), types.Tool( name="playwright_get_html_content", description="Get the HTML content of the page", inputSchema={ "type": "object", "properties": { "selector": {"type": "string", "description": "CSS selector for the element"} }, "required": ["selector"] } ) ]