Skip to main content
CodeAgent writes and executes Python code locally with browser automation capabilities. It’s designed for repetitive data extraction tasks where the agent can write reusable functions.
CodeAgent executes Python code on your local machine like Claude Code.

Quick Start

import asyncio
from browser_use import CodeAgent
from dotenv import load_dotenv

load_dotenv()

async def main():
    task = "Extract all products from example.com and save to products.csv"

    agent = CodeAgent(task=task)
    await agent.run()

asyncio.run(main())
.env
BROWSER_USE_API_KEY=your-api-key
CodeAgent currently only works with ChatBrowserUse which is optimized for this use case. Don’t have one? We give you $10 to try it out here.

When to Use

Best for:
  • Data extraction at scale (100s-1000s of items)
  • Repetitive interactions where functions can be reused
  • Tasks requiring data processing and file operations
  • Deterministic workflows you want to rerun
Performance:
  • Best performance for data collection tasks
  • Slightly slower for one-off interactions vs standard Agent
Output:
  • Generates Python code that can be rerun deterministically
  • Perfect for refining extraction logic
The agent will write code blocks in different languages. This combines the power of js for browser interaction and python for data processing:
extract_products
(function(){
  return Array.from(document.querySelectorAll('.product')).map(p => ({
    name: p.querySelector('.name').textContent,
    price: p.querySelector('.price').textContent
  }))
})()
import pandas as pd

products = await evaluate(extract_products) # reuse other code blocks
df = pd.DataFrame(products)
df.to_csv('products.csv', index=False)

Available Libraries

The agent can use common Python libraries:
  • Data processing: pandas, numpy
  • Web: requests, BeautifulSoup
  • File formats: csv, json, openpyxl (Excel)
  • Visualization: matplotlib
  • Utilities: tabulate, datetime, re
  • and all which you install …

Available Tools

The agent has access to browser control functions:
  • navigate(url) - Navigate to a URL
  • click(index) - Click an element by index
  • input(index, text) - Type text into an input
  • scroll(down, pages) - Scroll the page
  • upload_file(path) - Upload a file
  • evaluate(code, variables={}) - Execute JavaScript and return results
  • done(text, success, files_to_display=[]) - Mark task complete

Exporting Sessions

CodeAgent automatically saves all executed code and JavaScript blocks during your session. You can export your complete automation workflow for sharing, version control, or re-running later.

Quick Export

from browser_use.code_use.notebook_export import export_to_ipynb, session_to_python_script

# After running your agent
await agent.run()

# Export to Jupyter notebook
notebook_path = export_to_ipynb(agent, "my_automation.ipynb")

# Export to Python script
script = session_to_python_script(agent)
with open("my_automation.py", "w") as f:
    f.write(script)

Export Formats

  • Jupyter Notebook (.ipynb): Interactive development, sharing, documentation
  • Python Script (.py): Production deployment, version control, automation
Both formats include:
  • Setup code with browser initialization
  • JavaScript code blocks as Python variables
  • All executed Python cells with outputs
  • Ready-to-run automation workflows