How it works
When you execute Playwright code through this API:- Your code runs directly in the browser’s VM (no CDP overhead)
- You have access to
page,context, andbrowservariables - You can
returna value, which is returned in the response - Execution is isolated in a fresh context each time
Quick example
Available variables
Your code has access to these Playwright objects:page- The current page instancecontext- The browser contextbrowser- The browser instance
Returning values
Use areturn statement to send data back from your code:
Timeout configuration
Set a custom timeout (default is 60 seconds, max is 300 seconds):Error handling
The response includes error information if execution fails:Use cases
Web scraping
Extract data from multiple pages without CDP overhead:Form automation
Fill and submit forms quickly:Testing and validation
Run quick checks against your browser state:Screenshots
Capture screenshots using Playwright’s native screenshot API:For OS-level screenshots using coordinates and regions, see Computer Controls.
Performance benefits
Compared to connecting over CDP:- Lower latency - Code runs in the same VM as the browser
- Higher throughput - No websocket overhead for commands
- Simpler code - No need to manage CDP connections
MCP server integration
This feature is available as a tool in our MCP server. AI agents can use theexecute_playwright_code tool to run Playwright code against browsers with automatic video replay and cleanup.
