Skip to main content
Glama
blackwhite084

Playwright Server MCP

playwright_get_text_content

Extract text content from web page elements for web automation tasks using Playwright, enabling data collection and content analysis.

Instructions

Get the text content of all elements

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The GetTextContentToolHandler class that executes the tool logic: checks for active session, evaluates JavaScript to extract unique text from visible elements with <=3 children and their values, returns the list of texts.
    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(): defines name, description, and empty inputSchema (no 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 'playwright_get_text_content' to an instance of GetTextContentToolHandler.
    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(),
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries full burden but provides minimal behavioral information. It doesn't disclose whether this is a read-only operation, what happens if no elements exist, performance characteristics, or what format the text content is returned in. The phrase 'all elements' is vague without context.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without any unnecessary words. It's appropriately sized for a simple tool with no parameters.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with no annotations, no output schema, and no parameters, the description is insufficient. It doesn't explain what 'all elements' refers to (current page? specific selector?), what format the text is returned in, or how this differs from similar sibling tools. The agent lacks necessary context for proper use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The tool has zero parameters with 100% schema description coverage, so the description doesn't need to compensate for missing parameter documentation. The baseline for zero parameters is 4, as there are no parameters to explain beyond what's already covered.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Get') and target ('text content of all elements'), which is specific and unambiguous. However, it doesn't differentiate from sibling tools like 'playwright_get_html_content' or explain what 'all elements' means in context.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives like 'playwright_get_html_content' or 'playwright_evaluate'. The description doesn't mention prerequisites, context requirements, or typical use cases.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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

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