Overview
Step type:execute_javascript
Browser Code uses an LLM (Claude) to generate JavaScript based on your prompt, then executes it directly in the browser.
How It Works
- Code Generation: LLM generates JavaScript based on your natural language prompt
- Execution: Code is evaluated directly in the browser context
- Variable Support: Supports variable injection using
{{variable_name}}syntax - Async Support: Can await promises and return values
- Caching: Caches generated code for repeated execution
- Timeout: 120 seconds, 3 retry attempts
When to Use
Perfect for:
- Custom data extraction from complex page structures
- Bulk operations on page elements
- Direct DOM manipulation
- Advanced logic too specific for general agents
- Reliable, repeatable operations
- Structured data output requirements
- Simple interactions (use Browser Actions instead)
- Multi-page workflows (use Browser Agents instead)
- When you need the agent to navigate autonomously
- When the page structure is highly dynamic and unpredictable
Cost
Medium - includes LLM code generation cost plus execution cost.Example Use Cases
Data Extraction
Extract structured data from tables:Form Auto-Fill
Fill forms with dynamic data:Price Calculation
Calculate totals from page elements:Link Extraction
Find and return specific links:Complex Validation
Custom validation logic:Configuration Options
return_by_value
Get the result value from the code execution:false if you only need side effects (like clicking or typing) without a return value.
await_promise
Wait for async operations to complete:fetch, setTimeout, or other promises.
Structured Output
Browser Code excels at returning structured data:Array Output
Object Output
Complex Nested Output
Best Practices
Do ✅
- Be specific about the data structure you want
- Use
return_by_valuewhen you need output - Leverage caching for repeated operations
- Use variables for dynamic content
- Request structured output (arrays, objects)
- Specify exact field names for data extraction
Don’t ❌
- Use for multi-page navigation
- Expect it to handle complex user interactions
- Use when simple Browser Actions would work
- Forget to set
await_promisefor async code - Use for tasks requiring visual understanding
Writing Effective Prompts
❌ Vague Prompt
✅ Specific Prompt
❌ Unclear Output
✅ Clear Output Format
Common Patterns
Table Extraction
Form Data Collection
Element Counting
Conditional Extraction
Error Handling
Browser Code includes automatic retry logic:- 3 retry attempts for transient failures
- 120 second timeout per attempt
- Detailed error messages for debugging
- Is the page structure what you expect?
- Are the selectors/elements available?
- Is the prompt specific enough?
- Do you need
await_promisefor async code?
When to Switch
Consider switching to Browser Actions when:- You need simple interactions, not data extraction
- Natural language instructions are sufficient
- You want faster development time
- The task is simple enough for 2 steps
- You need to navigate multiple pages
- The workflow requires adaptive decision-making
- You need autonomous behavior
- Page structure is unpredictable
Comparison: Browser Code vs Browser Assess Page
For data extraction, you can also usebrowser_assess_page:
Browser Code
- Generates JavaScript code
- More precise and reliable
- Better for complex extraction logic
Browser Assess Page
- Uses LLM to understand page content
- Simpler for straightforward extraction
- Schema validation built-in