Launch or connect to an existing browser and configure it to your needs.
BrowserSession(...)
:
BrowserSession
& BrowserProfile
accept all the same arguments that Playwright’s launch_persistent_context(...)
takes, giving you full control over browser settings at launch. (see below for the full list)BrowserSession
BrowserSession(**params)
is Browser Use’s object that tracks a connection to a running browser. It sets up:
playwright
, browser
, browser_context
, and page
objects and tracks which tabs the agent/human are focused onDOMService
for element detectionbrowser_profile=BrowserProfile(...)
template containing some config defaults, and **kwargs
session-specific config overridesBrowserProfile(...)
template.
wss_url
cdp_url
http://localhost:9222
). See here for CDP connection instructions.
browser_pid
browser_profile
BrowserProfile
template containing default config to use for the BrowserSession
. (see below for more info)
**kwargs
BrowserSession
can also accept all of the parameters below.
(the parameters above this point are specific to BrowserSession
and cannot be stored in a BrowserProfile
template)
Extra **kwargs
passed to BrowserSession(...)
act as session-specific overrides to the BrowserProfile(...)
template.
BrowserProfile
BrowserProfile
is a 📋 config template for a 🎭 BrowserSession(...)
.
It’s basically just a typed + validated version of a dict
to hold config.
When you find yourself storing or re-using many browser configs, you can upgrade from:
BrowserProfile
, you can always pass config parameters directly to BrowserSession
:BrowserProfile
is optional, but it provides a number of benefits over a normal dict
for holding config:
BrowserProfiles
s are designed to easily be given 🆔 uuid
s and put in a database + made editable by users.
BrowserSession
s get their own 🆔 uuid
s and be linked by 🖇 foreign key to whatever BrowserProfiles
they use.This cleanly separates the per-connection rows from the bulky re-usable config and avoids wasting space in your db.
This is useful because a user may only have 2 or 3 profiles, but they could have 100k+ sessions within a few months.BrowserProfile
and BrowserSession
can both take any of the:
playwright
)BrowserProfile
can NOT take are the session-specific connection parameters and live playwright objects:cdp_url
, wss_url
, browser_pid
, page
, browser
, browser_context
, playwright
, etc.
BrowserSession(...)
and/or stored in a BrowserProfile
template.
keep_alive
True
it wont close the browser after the first agent.run()
ends. Useful for running multiple tasks with the same browser instance. If this is left as None
and the Agent launched its own browser, the default is to close the browser after the agent completes. If the agent connected to an existing browser then it will leave it open.
stealth
True
to use patchright
to avoid bot-blocking. (Might cause issues with some sites, requires manual testing.)
allowed_domains
['google.com', '*.wikipedia.org']
- Here the agent will only be able to access google.com
exactly and wikipedia.org
+ *.wikipedia.org
.
Glob patterns are supported:
['example.com']
✅ will match only https://example.com/*
exactly, subdomains will not be allowed.
It’s always the most secure to list all the domains you want to give the access to explicitly w/ schemes e.g.
['https://google.com', 'http*://www.google.com', 'https://myaccount.google.com', 'https://mail.google.com', 'https://docs.google.com']
['*.example.com']
⚠️ CAUTION this will match https://example.com
and all its subdomains.
Make sure all the subdomains are safe for the agent! abc.example.com
, def.example.com
, …, useruploads.example.com
, admin.example.com
disable_security
True
is NOT RECOMMENDED.bypass_csp=True
instead.
deterministic_rendering
True
is NOT RECOMMENDED.--deterministic-mode
--js-flags=--random-seed=1157259159
--force-color-profile=srgb
--font-render-hinting=none
--force-device-scale-factor=2
--enable-webgl
highlight_elements
viewport_expansion
-1
: All elements from the entire page will be included, regardless of visibility (highest token usage but most complete).0
: Only elements which are currently visible in the viewport will be included.500
(default): Elements in the viewport plus an additional 500 pixels in each direction will be included, providing a balance between context and token usage.include_dynamic_attributes
minimum_wait_page_load_time
wait_for_network_idle_page_load_time
maximum_wait_page_load_time
wait_between_actions
cookies_file
storage_state
instead, it’s the standard playwright format and also supports localStorage
and indexedDB
!The library will automatically save a new storage_state.json
next to any cookies_file
path you provide, just use `storage_state=‘path/to/storage_state.json’ to switch to the new format:cookies_file.json
: [{cookie}, {cookie}, {cookie}]
storage_state.json
: {"cookies": [{cookie}, {cookie}, {cookie}], "origins": {... optional localstorage state ...}}
Or run playwright open https://example.com/ --save-storage=storage_state.json
and log into any sites you need to generate a fresh storage state file.profile_directory
user_data_dir
(e.g. Default
, Profile 1
, Work
, etc.).
No need to set this unless you have multiple profiles set up in a single user_data_dir
and need to use a specific one.
window_position
save_recording_path
trace_path
{trace_path}/{context_id}.zip
.
BrowserSession
and BrowserProfile
.
They are defined in browser_use/browser/profile.py
. See here for the official Playwright documentation for all of these options.
headless
headless=False
on a server with no monitor attached, the browser will fail to launch (use xvfb
+ vnc to give a headless server a virtual display you can remote control).
headless=False
is recommended for maximum stealth and is required for human-in-the-loop workflows.
channel
['chromium']
(default when stealth=False
), 'chrome'
(default when stealth=True
), 'chrome-beta'
, 'chrome-dev'
, 'chrome-canary'
, 'msedge'
, 'msedge-beta'
, 'msedge-dev'
, 'msedge-canary'
Don’t worry, other chromium-based browsers not in this list (e.g. brave
) are still supported if you provide your own executable_path
, just set it to chromium
for those.
executable_path
user_data_dir
None
to use an ephemeral temporary profile (aka incognito mode).
Multiple running browsers cannot share a single user_data_dir
at the same time. You must set it to None
or
provide a unique user_data_dir
per-session if you plan to run multiple browsers.
The browser version run must always be equal to or greater than the version used to create the user_data_dir
.
If you see errors like Failed to parse Extensions
or similar and failures when launching, you’re attempting to run an older browser with an incompatible user_data_dir
that’s already been migrated to a newer schema version.
args
ignore_default_args
True
to disable all default options (not recommended).
env
{'DISPLAY': '1'}
to use a specific X11 display.
chromium_sandbox
False
when running inside Docker
because Docker provides its own sandboxing can conflict with Chrome’s.
devtools
headless=False
).
slow_mo
timeout
accept_downloads
proxy
{"server": "http://proxy.com:8080", "username": "user", "password": "pass"}
.
permissions
storage_state
storage_state
documentation on how to use it.
This option is only applied when launching a new browser using the default builtin playwright chromium and user_data_dir=None
is set.
default_timeout
10000
if you want 10s).
default_navigation_timeout
30000
if you want 30s).
user_agent
playwright.devices
.
is_mobile
has_touch
geolocation
{"latitude": 59.95, "longitude": 30.31667}
locale
en-GB
, de-DE
, etc. Locale will affect the navigator.language
value, Accept-Language
request header value as well as number and date formatting rules.
timezone_id
'America/New_York'
or 'UTC'
).
window_size
{"width": 1920, "height": 1080}
viewport
width
and height
. Example: {"width": 1280, "height": 720}
no_viewport
viewport
setting above.
A viewport is always used in headless mode regardless of this setting, and is never used in headful mode unless you pass viewport={width, height}
explicitly.
device_scale_factor
screen
color_scheme
'light'
, 'dark'
, 'no-preference'
contrast
'no-preference'
, 'more'
, 'null'
reduced_motion
'reduce'
, 'no-preference'
, 'null'
forced_colors
'active'
, 'none'
, 'null'
**playwright.devices[...]
BrowserSession
and BrowserProfile
take all the standard playwright args, we are able to support these device presets as well.
See allowed_domains
above too!
offline
http_credentials
extra_http_headers
ignore_https_errors
bypass_csp
java_script_enabled
service_workers
'allow'
, 'block'
base_url
page.goto()
and similar operations.
strict_selectors
client_certificates
record_video_dir
.webm
video recordings. Playwright Docs: record_video_dir
save_recording_path
for backwards compatibility with past versions, but we recommend using the standard Playwright name record_video_dir
going forward.record_video_size
{"width": 1280, "height": 720}
record_har_path
.har
network trace files. Playwright Docs: record_har_path
save_har_path
for backwards compatibility with past versions, but we recommend using the standard Playwright name record_har_path
going forward.record_har_content
'omit'
, 'embed'
, 'attach'
record_har_mode
'full'
, 'minimal'
record_har_omit_content
record_har_url_filter
downloads_path
downloads_dir
, save_downloads_path
)
Local filesystem directory to save browser file downloads to.
traces_dir
{traces_dir}/{context_id}.zip
. Playwright Docs: traces_dir
trace_path
for backwards compatibility with past versions, but we recommend using the standard Playwright name traces_dir
going forward.handle_sighup
handle_sigint
handle_sigterm
browser_use/browser/session.py
) handles the live browser connection and runtime statebrowser_use/browser/profile.py
) is a template that can store default config parameters for a BrowserSession(...)
BrowserConnectArgs
- args for playwright.BrowserType.connect_over_cdp(...)
BrowserLaunchArgs
- args for playwright.BrowserType.launch(...)
BrowserNewContextArgs
- args for playwright.BrowserType.new_context(...)
BrowserLaunchPersistentContextArgs
- args for playwright.BrowserType.launch_persistent_context(...)