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
Update .gitignore and add example for running multiple browser agents in parallel
Magnus Müller5fe19f03f7e1
diff --git a/.gitignore b/.gitignoreindex bab0e6265..ffa0cf9ef 100644--- a/.gitignore+++ b/.gitignore@@ -53,3 +53,6 @@ credentials.jsontoken.json!docs/docs.json+++temp-profile-*\ No newline at end of filediff --git a/docs/customize/browser-multiple.mdx b/docs/customize/browser-multiple.mdxindex 5493ac23b..6ba099d02 100644--- a/docs/customize/browser-multiple.mdx+++ b/docs/customize/browser-multiple.mdx@@ -4,106 +4,44 @@ description: "Run multiple agents in parallel with separate browser instances"icon: "copy"----Run multiple browser-use agents simultaneously, each with their own isolated browser instance.--## Basic Example-```pythonimport asynciofrom browser_use import Agent, Browser, ChatOpenAIasync def main():- # Create 3 separate browser instances- browsers = [- Browser(- user_data_dir=f'./temp-profile-{i}',- headless=False, # Set to True for production- keep_alive=True,- )- for i in range(3)- ]-- # Create 3 agents with different tasks- agents = [- Agent(- task='Search for "browser automation" on Google',- browser=browsers[0],- llm=ChatOpenAI(model='gpt-4.1-mini'),- ),- Agent(- task='Search for "AI agents" on DuckDuckGo',- browser=browsers[1],- llm=ChatOpenAI(model='gpt-4.1-mini'),- ),- Agent(- task='Visit Wikipedia and search for "web scraping"',- browser=browsers[2],- llm=ChatOpenAI(model='gpt-4.1-mini'),- ),- ]-- print('🚀 Starting 3 agents in parallel...')-- # Run all agents in parallel- tasks = [agent.run() for agent in agents]- results = await asyncio.gather(*tasks, return_exceptions=True)-- # Print results- for i, result in enumerate(results):- if isinstance(result, Exception):- print(f'❌ Agent {i+1} failed: {result}')- else:- print(f'✅ Agent {i+1} completed successfully')-- # Clean up browsers- print('🧹 Cleaning up browsers...')- for browser in browsers:- await browser.stop()-- print('🎉 All agents completed!')--if __name__ == '__main__':- asyncio.run(main())+ # Create 3 separate browser instances+ browsers = [+ Browser(+ user_data_dir=f'./temp-profile-{i}',+ headless=False,+ )+ for i in range(3)+ ]++ # Create 3 agents with different tasks+ agents = [+ Agent(+ task='Search for "browser automation" on Google',+ browser=browsers[0],+ llm=ChatOpenAI(model='gpt-4.1-mini'),+ ),+ Agent(+ task='Search for "AI agents" on DuckDuckGo',+ browser=browsers[1],+ llm=ChatOpenAI(model='gpt-4.1-mini'),+ ),+ Agent(+ task='Visit Wikipedia and search for "web scraping"',+ browser=browsers[2],+ llm=ChatOpenAI(model='gpt-4.1-mini'),+ ),+ ]++ # Run all agents in parallel+ tasks = [agent.run() for agent in agents]+ results = await asyncio.gather(*tasks, return_exceptions=True)++ print('🎉 All agents completed!')```-## How it Works--1. **Separate Profiles**: Each browser gets a unique `user_data_dir` to avoid conflicts-2. **Parallel Execution**: `asyncio.gather()` runs all agents simultaneously-3. **Isolated Sessions**: Each agent operates independently-4. **Resource Management**: Browsers are properly cleaned up after completion--## Key Benefits--- ✅ **True Parallelism**: Multiple tasks execute simultaneously-- ✅ **Isolation**: Each browser has separate cookies, cache, and state-- ✅ **Scalability**: Add more browsers/agents as needed-- ✅ **Resource Efficiency**: Clean shutdown prevents memory leaks--## Best Practices--```python-# Use headless for production-browsers = [- Browser(- user_data_dir=f'./profile-{i}',- headless=True, # Better performance- keep_alive=True,- )- for i in range(5)-]--# Handle errors gracefully-results = await asyncio.gather(*tasks, return_exceptions=True)-for i, result in enumerate(results):- if isinstance(result, Exception):- print(f'Agent {i+1} error: {result}')--# Always clean up-finally:- for browser in browsers:- try:- await browser.stop()- except Exception as e:- print(f'Cleanup error: {e}')-```+> **Note:** This is experimental, and agents might conflict each other.diff --git a/examples/browser/parallel_browser.py b/examples/browser/parallel_browser.pynew file mode 100644index 000000000..9776d9c6a--- /dev/null+++ b/examples/browser/parallel_browser.py@@ -0,0 +1,45 @@+import asyncio++from browser_use import Agent, Browser, ChatOpenAI++# NOTE: This is still experimental, and agents might conflict each other.+++async def main():+ # Create 3 separate browser instances+ browsers = [+ Browser(+ user_data_dir=f'./temp-profile-{i}',+ headless=False,+ )+ for i in range(3)+ ]++ # Create 3 agents with different tasks+ agents = [+ Agent(+ task='Search for "browser automation" on Google',+ browser=browsers[0],+ llm=ChatOpenAI(model='gpt-4.1-mini'),+ ),+ Agent(+ task='Search for "AI agents" on DuckDuckGo',+ browser=browsers[1],+ llm=ChatOpenAI(model='gpt-4.1-mini'),+ ),+ Agent(+ task='Visit Wikipedia and search for "web scraping"',+ browser=browsers[2],+ llm=ChatOpenAI(model='gpt-4.1-mini'),+ ),+ ]++ # Run all agents in parallel+ tasks = [agent.run() for agent in agents]+ results = await asyncio.gather(*tasks, return_exceptions=True)++ print('🎉 All agents completed!')+++if __name__ == '__main__':+ asyncio.run(main())