Skip to content

Eval API

Execute code

POST /eval/{sid}
{
"code": "result = await page.title()\nprint(result)",
"timeout": 30000
}

Executes arbitrary Python code in a sandboxed environment with access to the session’s Playwright objects.

Sandbox globals

NameTypeDescription
pageplaywright.async_api.PageActive page
contextplaywright.async_api.BrowserContextBrowser context
browserplaywright.async_api.BrowserBrowser instance
statedictSession state (console_logs, network_requests, etc.)
pageslistAll pages in the session
asynciomoduleasyncio module

Response:

{
"result": "Example Domain",
"stdout": ""
}

result is the value of the last expression evaluated (if any). stdout captures any print() output.

Examples

Get page title:

await page.title()

Extract all links:

links = await page.evaluate("""
() => Array.from(document.querySelectorAll('a[href]'))
.map(a => ({text: a.innerText.trim(), href: a.href}))
""")
result = links

Inject a cookie:

await context.add_cookies([{
"name": "session",
"value": "abc123",
"domain": "example.com",
"path": "/"
}])

Grant permissions:

await context.grant_permissions(["geolocation"])
await page.set_geolocation({"latitude": 48.1, "longitude": 17.1})

!!! warning “No stdout return by default” eval does not return printed output unless you use print() explicitly and check stdout. Side effects (cookies set, navigation triggered) take effect immediately but aren’t reported unless you capture them in result.

!!! note “Async functions” The sandbox is async — you can await any Playwright coroutine directly.