Skip to main content
Glama
ziux

Playwright Server MCP

by ziux

playwright_get_text_content

Extract and filter visible text content from web pages using browser automation, removing duplicates for clean and actionable data output.

Instructions

获取当前页面中所有可见元素的文本内容,智能过滤重复内容

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The async handle method that implements the tool logic: evaluates JavaScript on the page to collect unique text from visible elements with few children, filters by length, and returns the list of texts.
    async def handle(self, name: str, arguments: dict | None) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
        logger.info("开始获取页面文本内容")
        if not self._sessions:
            logger.warning("没有活跃的会话。需要先创建一个新会话。")
            return [types.TextContent(type="text", text="No active session. Please create a new session first.")]
        try:
            session_id = list(self._sessions.keys())[-1]
            page = self._sessions[session_id]["page"]
            logger.debug(f"从页面获取文本, URL: {page.url}")
            # text_contents = await page.locator('body').all_inner_texts()
    
            async def get_unique_texts_js(page):
                logger.debug("执行JavaScript获取唯一文本")
                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)
            logger.info(f"获取到 {len(text_contents)} 个唯一文本元素")
            logger.debug(f"文本内容: {text_contents[:3]}...")
    
            return [types.TextContent(type="text", text=f"Text content of all elements: {text_contents}")]
        except Exception as e:
            logger.error(f"获取文本内容失败: {str(e)}", exc_info=True)
            return [types.TextContent(type="text", text=f"获取文本内容失败: {str(e)}")]
  • Class definition with tool name, description, and input schema (empty, no parameters required). This is used by base.to_tool() to generate JSON schema for MCP.
    class GetTextContentToolHandler(ToolHandler):
        name = "playwright_get_text_content"
        description = "获取当前页面中所有可见元素的文本内容,智能过滤重复内容"
        inputSchema = []
  • Registers the GetTextContentToolHandler instance in the tool_handler_list, which is mapped to a dict by name for lookup in MCP tool calls.
    tool_handler_list = [
        NavigateToolHandler(),
        # ScreenshotToolHandler(),
        EvaluateToolHandler(),
        GetTextContentToolHandler(),
        GetHtmlContentToolHandler(),
        NewSessionToolHandler(),
        ActionToolHandler()
    ]
Behavior2/5

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

No annotations are provided, so the description carries the full burden. It mentions '智能过滤重复内容' (intelligently filter duplicate content), which adds some behavioral context about output processing. However, it lacks critical details: whether this requires a page to be loaded, what 'visible' means (e.g., viewport vs. DOM), error handling, performance implications, or output format. For a tool with no annotations, this is insufficient disclosure.

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 in Chinese: '获取当前页面中所有可见元素的文本内容,智能过滤重复内容'. It is front-loaded with the core purpose and adds a useful behavioral note ('智能过滤重复内容') without redundancy. Every word earns its place, making it appropriately concise.

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?

Given no annotations, no output schema, and moderate complexity (involving page interaction and text processing), the description is incomplete. It doesn't explain what 'visible' entails, how duplicates are filtered, the return format (e.g., string, array), or error cases. For a tool that likely interacts with a browser/page, more context is needed for safe and effective 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 0 parameters, and schema description coverage is 100% (empty schema). The description doesn't need to explain parameters, so it meets the baseline of 4 for zero-parameter tools. No additional parameter information is required or provided.

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 tool's purpose: '获取当前页面中所有可见元素的文本内容' (get text content of all visible elements on the current page). It specifies the verb ('获取' - get) and resource ('文本内容' - text content), and distinguishes from sibling tools like playwright_get_html_content by focusing on text rather than HTML. However, it doesn't explicitly differentiate from playwright_evaluate which might also retrieve text content.

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

Usage Guidelines3/5

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

The description implies usage context: it should be used when you need text content from visible elements on a page, and the '智能过滤重复内容' (intelligently filter duplicate content) suggests it's preferable when dealing with redundant text. However, it doesn't explicitly state when NOT to use it or name alternatives among siblings (e.g., playwright_get_html_content for HTML, playwright_evaluate for custom JavaScript).

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

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

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