browser-evaluate
Execute JavaScript code directly in the browser and retrieve results. Supports expressions, functions, async/await, and targeting specific DOM elements for precise evaluation.
Instructions
Evaluates JavaScript code directly in the browser context and returns the result. Supports expressions, function strings, and complex code with automatic execution handling. Can target the entire page or work with element handles for precise DOM manipulation.
Examples:
Simple expression: "document.title"
Arrow function: "() => document.querySelectorAll('a').length"
Regular function: "function() { return document.body.children.length; }"
Function with arguments: "(tag) => document.getElementsByTagName(tag).length"
Complex code: "() => { const divs = document.querySelectorAll('div'); return { count: divs.length, hasClass: divs[0]?.className }; }"
Async function: "async () => { const res = await fetch('/api'); return res.json(); }"
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| function | Yes | JavaScript code to execute (expression or function string). Examples: - Expression: "document.title" - Arrow function: "() => document.querySelectorAll('a').length" - Regular function: "function() { return document.body.children.length; }" - With arguments: "(tag) => document.getElementsByTagName(tag).length" - Async function: "async () => { const res = await fetch('/api'); return res.json(); }" | |
| args | No | Optional arguments to pass to the JavaScript function. Can be primitives, objects, arrays, or element references. Example: { url: "https://example.com", count: 5 } | |
| element | No | CSS selector to target specific element for evaluation | |
| contextId | No | Browser ID to execute on (uses most recent browser if not provided) | |
| timeout | No | Execution timeout in milliseconds (default: 30000) | |
| returnType | No | Expected return type for better serialization (default: auto) |