playwright_screenshot
Capture screenshots of web pages or specific elements using CSS selectors for documentation, testing, or visual verification purposes.
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-236 (handler)Implements the core logic for the 'playwright_screenshot' tool: checks for active session, locates element or full page, takes screenshot, base64 encodes it, and returns 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")]
- JSON Schema definition for the tool's input parameters returned by list_tools(): requires 'name', optional 'selector'.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:334-344 (registration)Registers the ScreenshotToolHandler instance in the tool_handlers dictionary, which is used by the @server.call_tool() handler to dispatch tool calls.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(), }