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
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | CSS选择器,用于定位需要获取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内容的页面元素") ]
- src/playwright_server/server.py:43-55 (registration)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}