Overview
TheBrowser (alias for BrowserSession) class manages the browser instance and provides both event-driven and imperative interfaces for browser control. It handles browser lifecycle, tab management, page interactions, and state tracking.
Browser and BrowserSession are the same class. Use Browser for cleaner, more intuitive code.Architecture
The browser session uses a 2-layer architecture:Event-Driven Layer
High-level events for agents and tools (navigate, click, type, etc.)
CDP Layer
Direct Chrome DevTools Protocol access for low-level control
Session Manager
Manages multiple targets (tabs, iframes) and CDP sessions
State Tracking
Captures browser state, DOM, and screenshots for the agent
Creating a Browser
Basic Usage
Browser Modes
- Local Browser
- Cloud Browser
- Remote CDP
- Real Browser Profile
Launch a local Chrome/Chromium instance:
Browser Session Lifecycle
Initialization
TheBrowserSession class (line 94 in browser/session.py) initializes in stages:
Startup Flow
Browser initialization is lazy - the actual browser doesn’t start until you use it in an async context or call an async method.
Context Manager Usage
Browser Configuration
Display Settings
Control how the browser appears:Set
headless=None for auto-detection based on display availability (useful for CI/CD).Security & Domains
Restrict navigation for safety:'example.com'→ Onlyhttps://example.com/*'*.example.com'→ Any subdomain including root'http*://example.com'→ Both HTTP and HTTPS- Wildcards in TLD (e.g.,
example.*) are not allowed for security
Lists with 100+ domains are automatically optimized to hash sets for O(1) lookup performance.
User Data & Profiles
Persist browser data across sessions:Network Configuration
Control network behavior:Downloads & Files
Configure file handling:Browser Profile
TheBrowserProfile class (line 44 in browser/session.py) encapsulates reusable configuration:
Profile Components
- Launch Args
- Context Args
- Recording
- Timing
Control browser process launch:
Target and Session Management
Understanding Targets
A Target (line 61 inbrowser/session.py) represents a browsing context:
- Page: Top-level browser tab
- Iframe: Embedded frame within a page
- Worker: Service/web worker
- Background: Extension background page
CDP Sessions
A CDPSession (line 76 inbrowser/session.py) is a communication channel:
Tab Management
Browser State Capture
State Summary
The agent usesget_browser_state_summary() to understand the page:
DOM State Structure
Browser Events
The event bus handles browser actions:Navigation Events
Available Events
NavigateToUrlEvent
Navigate to URL (optionally in new tab)
ClickElementEvent
Click element by DOM node
TypeTextEvent
Type text into input field
ScrollEvent
Scroll page or element
SwitchTabEvent
Switch to different tab
CloseTabEvent
Close a browser tab
UploadFileEvent
Upload file to input[type=file]
GoBackEvent
Navigate back in history
Actor Integration
The browser provides direct access to the Actor API for deterministic automation:The Actor API provides Playwright-like methods for precise browser control. See Actor documentation for complete reference.
Advanced Features
Element Highlighting
Visual feedback for debugging:Cross-Origin Iframes
Enable iframe support (may add complexity):Paint Order Filtering
Optimize DOM by removing hidden elements:Paint order filtering is experimental but can significantly reduce DOM size by removing occluded elements.
Demo Mode
Visual agent panel for live viewing:Cookie Management
Getting Cookies
Setting Cookies
Real-World Example
Performance Tips
- Optimize Screenshots
- Reduce DOM Size
Troubleshooting
Common Issues
Browser won't start
Browser won't start
Check:
- Chromium installed:
uvx browser-use install - Correct executable path if using custom browser
- User data directory not locked by another process
Connection timeout
Connection timeout
Solutions:
- Increase CDP timeout
- Check firewall/network settings
- Verify CDP URL is accessible
Element not found
Element not found
Debug:
- Enable
highlight_elements=True - Check page has fully loaded
- Verify element is not in iframe
Next Steps
Tools System
Learn about browser actions and tools
Actor API
Direct browser control methods
Browser Parameters
Complete parameter reference
Cloud Browser
Production deployment with cloud browsers