mirror of
https://github.com/browser-use/browser-use
synced 2026-05-06 17:52:15 +02:00
98 lines
2.7 KiB
Plaintext
98 lines
2.7 KiB
Plaintext
---
|
|
title: "Basics"
|
|
description: "Write Python code locally with browser automation"
|
|
icon: "code"
|
|
---
|
|
|
|
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.
|
|
|
|
<Warning>
|
|
CodeAgent executes Python code on your local machine like Claude Code.
|
|
</Warning>
|
|
|
|
## Quick Start
|
|
|
|
|
|
```python
|
|
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())
|
|
```
|
|
|
|
```bash .env
|
|
BROWSER_USE_API_KEY=your-api-key
|
|
```
|
|
|
|
<Note>
|
|
CodeAgent currently only works with [ChatBrowserUse](https://chat.browser-use.com) which is optimized for this use case.
|
|
Don't have one? We give you $10 to try it out [here](https://cloud.browser-use.com/dashboard/api).
|
|
</Note>
|
|
|
|
## 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:
|
|
```js extract_products
|
|
(function(){
|
|
return Array.from(document.querySelectorAll('.product')).map(p => ({
|
|
name: p.querySelector('.name').textContent,
|
|
price: p.querySelector('.price').textContent
|
|
}))
|
|
})()
|
|
```
|
|
```python
|
|
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
|