Skip to main content
Glama
ziux

Playwright Server MCP

by ziux

playwright_get_html_content

Extract the HTML content of a specific webpage element using a CSS selector through browser automation on the Playwright Server MCP.

Instructions

获取页面中指定元素的HTML内容

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
selectorYesCSS选择器,用于定位需要获取HTML内容的页面元素

Implementation Reference

  • The handle method of GetHtmlContentToolHandler executes the tool logic: retrieves the current page, locates the element by CSS selector, fetches its inner HTML, and returns it as text content.
    async def handle(self, name: str, arguments: dict | None) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
        logger.info("开始获取HTML内容")
        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"]
            selector = arguments.get("selector")
            logger.debug(f"获取选择器 '{selector}' 的HTML内容")
            html_content = await page.locator(selector).inner_html()
            logger.debug(f"获取到HTML内容,长度: {len(html_content)}")
            return [types.TextContent(type="text", text=f"HTML content of element with selector {selector}: {html_content}")]
        except Exception as e:
            logger.error(f"获取HTML内容失败: {str(e)}", exc_info=True)
            return [types.TextContent(type="text", text=f"获取HTML内容失败: {str(e)}")]
  • Tool name, description, and input schema defining the required 'selector' parameter as a CSS selector string.
    name = "playwright_get_html_content"
    description = "获取页面中指定元素的HTML内容"
    inputSchema = [
        Property(name="selector", typ="string", description="CSS选择器,用于定位需要获取HTML内容的页面元素")
    ]
  • The tool handler is instantiated and added to tool_handler_list, then mapped to a dictionary by name for lookup in the MCP server.
    tool_handler_list = [
        NavigateToolHandler(),
        # ScreenshotToolHandler(),
        EvaluateToolHandler(),
        GetTextContentToolHandler(),
        GetHtmlContentToolHandler(),
        NewSessionToolHandler(),
        ActionToolHandler()
    ]
    
    # 根据每个处理程序的 name 属性创建字典
    tool_handlers = {handler.name: handler for handler in tool_handler_list}
Behavior2/5

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

With no annotations provided, the description carries full burden but offers minimal behavioral context. It states what the tool does but doesn't disclose whether it's read-only/destructive, requires authentication, has rate limits, returns errors for invalid selectors, or what format the HTML content returns (full element vs inner HTML). The description doesn't contradict annotations since none exist, but provides inadequate behavioral transparency for a tool interacting with a browser/page.

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 Chinese sentence that directly states the tool's purpose without unnecessary words. It's appropriately sized for a simple tool with one parameter and is front-loaded with the core functionality. Every word earns its place.

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 and no output schema, the description is incomplete for a tool that retrieves content from a page. It doesn't explain what happens if the selector doesn't match, whether it returns inner/outer HTML, if it waits for elements to load, or the return format. For a Playwright tool interacting with dynamic pages, more context about behavior and output is needed.

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

Parameters3/5

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

Schema description coverage is 100% with one parameter clearly documented in the schema. The description adds no additional parameter semantics beyond what the schema provides ('CSS选择器,用于定位需要获取HTML内容的页面元素'). It doesn't explain selector syntax examples, multiple element handling, or timeout behavior. Baseline 3 is appropriate since the schema adequately covers the single parameter.

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 verb ('获取' meaning 'get') and resource ('HTML内容' meaning 'HTML content') with specific targeting ('指定元素' meaning 'specified element'). It distinguishes from sibling playwright_get_text_content by specifying HTML vs text content. However, it doesn't explicitly differentiate from playwright_evaluate which might also retrieve content.

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?

The description provides no guidance on when to use this tool versus alternatives like playwright_get_text_content or playwright_evaluate. There's no mention of prerequisites (e.g., needing an active session), performance considerations, or error conditions. The agent must infer usage from the tool name and description alone.

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