playwright_evaluate
Execute JavaScript in browser console using Chrome DevTools Protocol via Playwright, enabling advanced web interactions and automation.
Instructions
Execute JavaScript in the browser console
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/toolsHandler.ts:272-312 (handler)The main handler logic for the 'playwright_evaluate' tool. It uses Playwright's page.evaluate to run the provided JavaScript script in the browser page context. It captures console logs by overriding console methods temporarily, executes the script using eval, and returns the result along with captured logs in a formatted text response.case "playwright_evaluate": try { const result = await page!.evaluate((script) => { const logs: string[] = []; const originalConsole = { ...console }; ['log', 'info', 'warn', 'error'].forEach(method => { (console as any)[method] = (...args: any[]) => { logs.push(`[${method}] ${args.join(' ')}`); (originalConsole as any)[method](...args); }; }); try { const result = eval(script); Object.assign(console, originalConsole); return { result, logs }; } catch (error) { Object.assign(console, originalConsole); throw error; } }, args.script); return { content: [ { type: "text", text: `Execution result:\n${JSON.stringify(result.result, null, 2)}\n\nConsole output:\n${result.logs.join('\n')}`, }, ], isError: false, }; } catch (error) { return { content: [{ type: "text", text: `Script execution failed: ${(error as Error).message}`, }], isError: true, }; }