Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
New examples
Magnus Müllerbe27ee8f2f31
diff --git a/docs/customize/code-agent/basics.mdx b/docs/customize/code-agent/basics.mdxnew file mode 100644index 000000000..4718e739f--- /dev/null+++ b/docs/customize/code-agent/basics.mdx@@ -0,0 +1,97 @@+---+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 completediff --git a/docs/customize/code-agent/example-products.mdx b/docs/customize/code-agent/example-products.mdxnew file mode 100644index 000000000..afd602f66--- /dev/null+++ b/docs/customize/code-agent/example-products.mdx@@ -0,0 +1,59 @@+---+title: "Example: Extract Products"+description: "Collect thousands of products and save to CSV"+icon: "database"+---++This example shows how to extract large amounts of product data from an e-commerce site and save it to files.++## Use Case++Extract 1000s of products from multiple categories with:+- Product URLs+- Names and descriptions+- Original and sale prices+- Discount percentages++Save everything to a CSV file for further analysis.++## Code++```python+import asyncio+from browser_use.code_use import CodeAgent++async def main():+ task = """+ Go to https://www.flipkart.com.+ Collect approximately 50 products from:++ 1. Books & Media - 15 products+ 2. Sports & Fitness - 15 products+ 3. Beauty & Personal Care - 10 products++ Save to products.csv+ """++ agent = CodeAgent(task=task)+ await agent.run()++asyncio.run(main())+```++## How It Works++1. **Agent navigates** to the e-commerce site+2. **Writes JavaScript** to extract product data from each page+3. **Loops through categories** collecting products+4. **Stores in variables** that persist across steps+5. **Saves to CSV** using pandas or csv module+6. **Returns deterministic code** you can modify and rerun++## Key Benefits++- **Function reuse:** Extraction code is written once, used many times+- **Scale:** Easily collect 100s or 1000s of items+- **Deterministic:** The generated Python code can be saved and rerun+- **Data processing:** Built-in pandas support for cleaning and transforming data++[View full example on GitHub →](https://github.com/browser-use/browser-use/blob/main/examples/code_agent/extract_products.py)diff --git a/docs/docs.json b/docs/docs.jsonindex 59bb260a9..6504ed143 100644--- a/docs/docs.json+++ b/docs/docs.json@@ -161,6 +161,15 @@"customize/agent/all-parameters"]},+ {+ "group": "Code Agent",+ "icon": "code",+ "isDefaultOpen": false,+ "pages": [+ "customize/code-agent/basics",+ "customize/code-agent/example-products"+ ]+ },{"group": "Browser","icon": "window",diff --git a/docs/quickstart.mdx b/docs/quickstart.mdxindex bc387ec4e..03a82f3a2 100644--- a/docs/quickstart.mdx+++ b/docs/quickstart.mdx@@ -45,8 +45,8 @@ pip install playwright && playwright install chromium --with-deps## 2. Choose your favorite LLMCreate a `.env` file and add your API key.-<Callout icon="key" color="#FFC107" iconType="regular">-Don't have one? Get **$10 free LLM credits** on [Browser Use Cloud 🔗](https://cloud.browser-use.com/dashboard/api).+<Callout icon="key" iconType="regular">+We recommend using ChatBrowserUse which is optimized for browser automation tasks (highest accuracy + fastest speed + lowest token cost). Don't have one? We give you **$10** to try it out [here](https://cloud.browser-use.com/dashboard/api).</Callout>```bash .env