Browser Use makes web automation simple with an AI-powered agent that understands natural language tasks. This guide covers essential patterns for everyday automation.
Start with a basic agent that performs web searches:
from browser_use import Agent, ChatBrowserUseimport asyncioasync def main(): agent = Agent( task="Go to Hacker News and find the top post", llm=ChatBrowserUse(), ) result = await agent.run() print(result.final_result())asyncio.run(main())
We recommend using ChatBrowserUse - it’s optimized for browser automation with the highest accuracy, fastest speed, and lowest token cost.
2
Understand the Results
The agent returns an AgentHistoryList with complete execution details:
history = await agent.run()# Access execution dataprint(history.final_result()) # Last extracted contentprint(history.is_done()) # True if task completedprint(history.urls()) # All visited URLsprint(history.action_names()) # Actions performedprint(history.total_duration_seconds()) # Total time
from browser_use import Agent, ChatBrowserUseagent = Agent( task="Navigate to wikipedia.org and search for 'Python programming'", llm=ChatBrowserUse(),)result = await agent.run()
agent = Agent( task=""" 1. Go to example.com/contact 2. Fill in the name field with 'John Doe' 3. Fill in email with 'john@example.com' 4. Fill in message with 'Hello world' 5. Click submit button """, llm=ChatBrowserUse(),)result = await agent.run()
Be specific about form fields. Reference them by their labels or placeholder text for best results.
agent = Agent( task=""" 1. Open GitHub in a new tab 2. Open Reddit in another new tab 3. Switch to the Reddit tab 4. Search for 'programming' 5. Switch back to GitHub tab """, llm=ChatBrowserUse(),)result = await agent.run()
from browser_use import Agent, ChatBrowserUseimport asyncioasync def main(): agent = Agent( task="Navigate to invalid-site.com", llm=ChatBrowserUse(), max_failures=5, # Retry up to 5 times on errors ) try: result = await agent.run() if result.has_errors(): print("Errors occurred:", result.errors()) else: print("Success:", result.final_result()) except Exception as e: print(f"Fatal error: {e}")asyncio.run(main())
agent = Agent( task=""" 1. Go to news.ycombinator.com 2. Use extract action to get the top 5 post titles and URLs """, llm=ChatBrowserUse(),)result = await agent.run()data = result.final_result()print(data)
Explicitly mention “use extract action” in your task for best results. See the Data Extraction guide for advanced patterns.
history = await agent.run()# Debugging informationprint(f"Completed: {history.is_done()}")print(f"Steps taken: {history.number_of_steps()}")print(f"Duration: {history.total_duration_seconds()}s")print(f"URLs visited: {history.urls()}")print(f"Actions: {history.action_names()}")# Get screenshots (if vision enabled)for i, screenshot in enumerate(history.screenshots()): with open(f'step_{i}.png', 'wb') as f: f.write(base64.b64decode(screenshot))