Overview
The Browser object is the root of the hierarchy and contains a reference to the browser parent process. There is usually only one instance of this class. All opened tabs, extra browser windows, and resources do not cause a new Browser process but rather create additional Tab objects. Besides starting your instance and managing tabs, you don’t actively use the Browser object frequently under normal conditions.Creating a Browser instance
The Browser object is not instantiated directly using__init__. Instead, use one of these asynchronous methods:
Class methods
create()
Create a new Browser instance.Configuration object. If provided, other parameters are ignored.
Directory for browser user data.
Launch in headless mode.
Custom user agent string.
Path to browser executable.
Browser type:
"chrome", "brave", or "auto".Additional browser command-line arguments.
Enable browser sandboxing.
Browser language.
Host for connecting to existing browser.
Port for connecting to existing browser.
Properties
main_tab
Returns the target that was launched with the browser.The main tab, or None if no tabs exist.
tabs
Returns all current tabs (targets of type “page”).List of all open tabs.
cookies
Access the browser’s cookie jar.CookieJar instance for managing cookies.
stopped
Check if the browser process has stopped.True if the browser process is not running.
websocket_url
Get the WebSocket debugger URL.WebSocket URL for debugging.
Methods
get()
Navigate to a URL using the first available tab.The URL to navigate to.
Open URL in a new tab.
Open URL in a new window.
The tab that navigated to the URL.
start()
Start the browser process.The Browser instance.
stop()
Stop the browser process and clean up resources.wait()
Wait for a specified amount of time.Time to wait in seconds.
The Browser instance.
sleep()
Alias forwait().
grant_all_permissions()
Grant all browser permissions.- accessibilityEvents
- audioCapture
- backgroundSync
- clipboardReadWrite
- geolocation
- notifications
- videoCapture
- And many more…
tile_windows()
Tile browser windows across the screen.Specific tabs to tile. If None, tiles all tabs.
Maximum number of columns. If 0, calculated automatically.
List of window positions [x, y, width, height].
update_targets()
Update the list of targets (tabs).Examples
Basic navigation
Open new tab
Manage cookies
Grant permissions
Context manager
Iterate over tabs
Notes
- Always call
await browser.stop()when finished to properly close the browser - The Browser object manages the parent browser process
- Multiple tabs share the same browser process
- Use context managers for automatic cleanup
- On Linux as root, sandbox is automatically disabled