playwright_screenshot
Capture screenshots of entire web pages or specific elements using CSS selectors for web automation and documentation 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 page or element, captures screenshot, base64 encodes it, cleans up temp file, 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")]
- Defines the JSON schema for input arguments (name required, optional selector) and description for the 'playwright_screenshot' tool in the list_tools() response.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 for 'playwright_screenshot' in the tool_handlers dictionary used by the call_tool handler.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(), }
- src/playwright_server/server.py:347-359 (registration)The call_tool handler that dispatches to the registered tool handler based on name, invoking ScreenshotToolHandler.handle() for 'playwright_screenshot'.@server.call_tool() async def handle_call_tool( name: str, arguments: dict | None ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: """ Handle tool execution requests. Tools can modify server state and notify clients of changes. """ if name in tool_handlers: return await tool_handlers[name].handle(name, arguments) else: raise ValueError(f"Unknown tool: {name}")