playwright_evaluate
Execute JavaScript directly in the browser console for automated testing or web interaction tasks using Playwright's browser automation capabilities.
Instructions
Execute JavaScript in the browser console
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/playwright_server/server.py:260-269 (handler)The EvaluateToolHandler class provides the core implementation for executing JavaScript code in the browser page using page.evaluate(). It retrieves the script from arguments, evaluates it, and returns the result as text content.class EvaluateToolHandler(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"] script = arguments.get("script") result = await page.evaluate(script) return [types.TextContent(type="text", text=f"Evaluated script, result: {result}")]
- The input schema definition for the playwright_evaluate tool, specifying that it requires a 'script' string parameter.types.Tool( name="playwright_evaluate", description="Execute JavaScript in the browser console", inputSchema={ "type": "object", "properties": { "script": {"type": "string", "description": "JavaScript code to execute"} }, "required": ["script"] } ),
- src/playwright_server/server.py:334-344 (registration)Registration of the EvaluateToolHandler instance in the tool_handlers dictionary, which maps tool names to their handler instances for execution in 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(), }