playwright_screenshot
Capture full-page or element-specific screenshots using CSS selectors for precise website testing or documentation.
Instructions
Take a screenshot of the current page or a specific element
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| selector | No | CSS selector for element to screenshot,null is full page |
Implementation Reference
- src/playwright_server/server.py:219-237 (handler)The ScreenshotToolHandler class implements the core logic for the playwright_screenshot tool. It captures a screenshot of the current page or a specific element using Playwright, saves it temporarily, encodes it to base64, and returns it as ImageContent.class ScreenshotToolHandler(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"] name = arguments.get("name") selector = arguments.get("selector") # full_page = arguments.get("fullPage", False) if selector: element = await page.locator(selector) await element.screenshot(path=f"{name}.png") else: await page.screenshot(path=f"{name}.png", full_page=True) with open(f"{name}.png", "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode("utf-8") os.remove(f"{name}.png") return [types.ImageContent(type="image", data=encoded_string, mimeType="image/png")]
- The JSON schema definition for the playwright_screenshot tool, specifying input parameters: name (required, string) and optional selector (CSS selector for element). Defined in the list_tools() handler.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"] } ),
- src/playwright_server/server.py:336-336 (registration)The playwright_screenshot tool is registered in the tool_handlers dictionary, mapping the tool name to an instance of ScreenshotToolHandler."playwright_screenshot": ScreenshotToolHandler(),