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
more docs improvements to clarify BrowserProfile
Nick Sweeting8b4e37b60485
diff --git a/docs/customize/browser-settings.mdx b/docs/customize/browser-settings.mdxindex c083e72f2..0b1b2e686 100644--- a/docs/customize/browser-settings.mdx+++ b/docs/customize/browser-settings.mdx@@ -1,94 +1,59 @@---title: "Browser Settings"-description: "Configure browser behavior and context settings"+description: "Launch or connect to an existing browser and configure it to your needs."icon: "globe"----Browser Use uses [playwright](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context) (or [patchright](https://github.com/Kaliiiiiiiiii-Vinyzu/patchright)) + CDP to manage its connection with a real browser.+Browser Use uses [playwright](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context) (or [patchright](https://github.com/Kaliiiiiiiiii-Vinyzu/patchright)) to manage its connection with a real browser.```python-from browser_use import BrowserProfile, BrowserSession-````-+from browser_use import BrowserSession, BrowserProfile, Agent+```-- `BrowserSession(**params)` is Browser Use's object that tracks a connection to a running browser. It holds:- - the `playwright`, `browser`, `browser_context`, and `page` objects and tracks which tabs the agent/human are focused on- - the helper methods to launch and connect to remote and local browsers- - methods to interact with the browser window, apply config needed by the Agent, and run the DOMService for element detection-- `BrowserProfile(**params)` is Browser Use's object that holds a collection of static config kwargs, to be used when starting a `BrowserSession`. It holds:- - all the standard playwright kwargs- - some extra browser configuration options we provide on top of playwright (e.g. `allowed_domains`, `window_position`, `profile_directory`, `deterministic_rendering`, etc.)- - some kwargs used to configure Browser Use-specific features related to the browser (e.g. `highlight_elements`, `disable_security`, `cookies_file`)-- [`Browser`](https://playwright.dev/python/docs/api/class-browser): standard [playwright `Browser`](https://playwright.dev/python/docs/api/class-browser) object, refers to a running browser process (remote or local)-- [`BrowserContext`](https://playwright.dev/python/docs/api/class-browsercontext): standard [playwright `BrowserContext`](https://playwright.dev/python/docs/api/class-browsercontext) refers to a window in a live browser (incognito or with a profile)-- [`Page`](https://playwright.dev/python/docs/api/class-page): standard [playwright `Page`](https://playwright.dev/python/docs/api/class-page), the handle for one tab in the browser+**To launch a new browser**, pass any playwright args you want to use to `BrowserSession(...)`:+```python+new_browser = BrowserSession(user_data_dir='~/Desktop/test-profile', headless=False, ...)+agent = Agent('fill out the form on this page', browser_session=new_browser)+```<Note>- The new `BrowserSession` and `BrowserProfile` classes now accept all the same arguments as standard Playwright [launch_persistent_context](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context) API, giving you full control over browser settings.+ The new `BrowserSession` & `BrowserProfile` accept all the same arguments that Playwright's [`launch_persistent_context(...)`](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context) takes, giving you full control over browser settings at launch. (see below for the full list)</Note>-# Browser Configuration+**To connect to an existing remote browser**, initialize a `BrowserSession` with a `cdp_url`, `wss_url`, or `browser_pid`:```python-from browser_use import Agent, BrowserSession, BrowserProfile--# It's easy to connect to or launch a new browser with a BrowserSession, many methods are supported:-browser_session = BrowserSession(- cdp_url='http://localhost:9222' | wss_url='ws://...',- # keep_alive=True,- # browser_pid=12445,- page=playwright_page | browser_context | browser | playwright,- # executable_path='...',- # user_data_dir='...',- # headless=True,- browser_profile=BrowserProfile(..., color_scheme='dark'),- # color_scheme='light', # <- will take precedence over ^- # ... other config overrides can go here-)--# you can optionally start a session and use it outside an Agent, otherwise Agent will automatically start it-await session.start()-page = await session.browser_context.new_page()-await page.goto('https://example.com/load-before-agent-starts')--agent = Agent(- task='your task here',- browser_session=browser_session, # provide a browser_session to the agent- page=page, # or as a shortcut, provide a playwright page directly-)+remote_browser = BrowserSession(cdp_url='http://localhost:9222', keep_alive=True)+agent = Agent(task='Do some task with a remote browser...', browser_session=remote_browser)+agent = Agent(task='Other task reusing the same browser...', browser_session=remote_browser)```-`BrowserProfile` is a static, flat collection of config.-It can be passed to a `BrowserSession` to start a session using that config.+**To use Browser Use from an existing playwright script** you can also pass an existing `page`, `browser_context`, or `browser` playwright objects to `BrowserSession(...)`.```python-browser_profile = BrowserProfile(- headless=True,- is_mobile=True,- **playwright.devices['iPhone 13'],- user_data_dir='~/Desktop/mobile_test_profile',- allowed_domains=['https://*.example.com'],-)+browser = await playwright.chromium.launch(...)-# create a new session using the profile-browser_session = BrowserSession(- browser_profile=browser_profile,- headless=False, # <- extra kwargs passed to session will override values from profile-)-```+page1 = await browser.new_page()+agent1 = Agent('Find the price for product abc', browser_session=BrowserSession(page=page1, keep_alive=True))+page2 = await browser.new_page()+agent2 = Agent('Find the price for product xyz', browser_session=BrowserSession(page=page2, keep_alive=True))-`BrowserSession` and `BrowserProfile` both accept the same long list of main config kwargs, most are standard playwright arguments that `playwright.BrowserType.launch_persistent_context()` takes.--We also provide some extra utility options to control Browser-Use-specific features and make setup easier,-e.g. `allowed_domains`, `keep_alive`, `highlight_elements`, and more.+# the agents will share the existing browser and each work on different tabs ^+```+---+## `BrowserSession`-## `BrowserSession` Parameters+- `BrowserSession(**params)` is Browser Use's object that tracks a connection to a running browser. It sets up:+ - the `playwright`, `browser`, `browser_context`, and `page` objects and tracks which tabs the agent/human are focused on+ - methods to interact with the browser window, apply config needed by the Agent, and run the `DOMService` for element detection+ - it can take extra `**kwargs` to pass to playwright or a `browser_profile=BrowserProfile(...)` containing some config+### Browser Connection Parameters-### Remote Browser Connection Parameters+Provide any one of these options to connect to an existing browser. These options are session-specific and cannot be stored in a `BrowserProfile(...)`.#### `wss_url`@@ -96,7 +61,7 @@ e.g. `allowed_domains`, `keep_alive`, `highlight_elements`, and more.wss_url: str | None = None```-WSS URL of the node.js playwright browser server to connect to+WSS URL of the playwright-protocol browser server to connect to. See here for [WSS connection instructions](https://docs.browser-use.com/customize/real-browser#method-d%3A-connect-to-remote-playwright-node-js-browser-server-via-wss-url).#### `cdp_url`@@ -104,7 +69,7 @@ WSS URL of the node.js playwright browser server to connect tocdp_url: str | None = None```-CDP URL of the browser to connect to (e.g., http://localhost:9222)+CDP URL of the browser to connect to (e.g. `http://localhost:9222`). See here for [CDP connection instructions](https://docs.browser-use.com/customize/real-browser#method-e%3A-connect-to-remote-browser-via-cdp-url).#### `browser_pid`@@ -112,7 +77,7 @@ CDP URL of the browser to connect to (e.g., http://localhost:9222)browser_pid: int | None = None```-PID of a running chromium-based browser process to connect to on localhost+PID of a running chromium-based browser process to connect to on localhost. See here for [connection via pid](https://docs.browser-use.com/customize/real-browser#method-c%3A-connect-to-local-browser-using-browser-pid) instructions.<Note>For web scraping tasks on sites that restrict automated access, we recommend@@ -120,7 +85,7 @@ PID of a running chromium-based browser process to connect to on localhostSee the [Connect to your Browser](real-browser) guide for detailed connection instructions.</Note>-### Runtime State / Parameters+### Session-Specific Parameters#### `browser_profile`@@ -128,7 +93,7 @@ PID of a running chromium-based browser process to connect to on localhostbrowser_profile: BrowserProfile = BrowserProfile()```-BrowserProfile instance containing config to use for the BrowserSession+Optional BrowserProfile instance containing config to use for the BrowserSession. (see below for more info)#### `playwright`@@ -136,8 +101,8 @@ BrowserProfile instance containing config to use for the BrowserSessionplaywright: Playwright | None = None```-Optional playwright or patchright API client object to use-result of (await async_playwright().start()) or (await async_patchright().start())+Optional playwright or patchright API client object to use, the+result of `(await async_playwright().start())` or `(await async_patchright().start())`. See here for [more detailed usage instructions](https://docs.browser-use.com/customize/real-browser#method-b%3A-connect-using-existing-playwright-objects).#### `browser`@@ -145,7 +110,7 @@ result of (await async_playwright().start()) or (await async_patchright().start(browser: Browser | None = None```-Playwright Browser object to use (optional)+Playwright Browser object to use (optional). See here for [more detailed usage instructions](https://docs.browser-use.com/customize/real-browser#method-b%3A-connect-using-existing-playwright-objects).#### `browser_context`@@ -153,36 +118,65 @@ Playwright Browser object to use (optional)browser_context: BrowserContext | None = None```-Playwright BrowserContext object to use (optional)+Playwright BrowserContext object to use (optional). See here for [more detailed usage instructions](https://docs.browser-use.com/customize/real-browser#method-b%3A-connect-using-existing-playwright-objects).-#### `initialized`+#### `page` *aka* `agent_current_page`++<a name="page"></a><a name="agent-current-page"></a>```python-initialized: bool = False+page: Page | None = None```-Mark BrowserSession as already initialized, skips launch/connection (not recommended)+Foreground Page that the agent is focused on, can also be passed as `page=...` as a shortcut. See here for [more detailed usage instructions](https://docs.browser-use.com/customize/real-browser#method-b%3A-connect-using-existing-playwright-objects).-#### `page` *aka* `agent_current_page`+#### `human_current_page````python-page: Page | None = None+human_current_page: Page | None = None```-Foreground Page that the agent is focused on, can also be passed as `page=...` as a shortcut.+Foreground Page that the human is focused on to start, usually not necessary to set manually.-#### `human_current_page`+#### `initialized````python-human_current_page: Page | None = None+initialized: bool = False```-Foreground Page that the human is focused on+Mark BrowserSession as already initialized, skips launch/connection (not recommended)++#### `**kwargs`++`BrowserSession` can also accept *all* of the parameters [below](#browserprofile).+(the parameters *above* this point are specific to `BrowserSession` and cannot be stored in a `BrowserProfile`)++----## Browser-Use Parameters-These parameters control browser-use specific features, and are outside the standard playwright parameter set.+## `BrowserProfile`++A `BrowserProfile` is an optional static collection of configuration that can be passed around and validated before it's used to start a `BrowserSession(browser_profile=BrowserProfile(...))`.++It can take all the same standard [playwright arguments](#playwright) that `BrowserSession(...)` takes, and some extras as well.++It allows you do do things like start multiple browsers with the same config easily:++```python+from browser_use.browser import BrowserProfile++browser_profile = BrowserProfile(headless=False, user_data_dir=None, allowed_domains=['https://*.example.com'], ...)++# start 3 separate browser instances with the same config options:+browser1 = BrowserSession(browser_profile=browser_profile, user_data_dir='/tmp/profile1')+browser2 = BrowserSession(browser_profile=browser_profile, user_data_dir='/tmp/profile2')+browser3 = BrowserSession(browser_profile=browser_profile, user_data_dir='/tmp/profile2')+```++### Browser-Use Parameters++These parameters control browser-use specific features, and are outside the standard playwright parameter set but are supported by both `BrowserSession` and `BrowserProfile`.#### `keep_alive`@@ -323,16 +317,32 @@ window_position: dict | None = {"width": 0, "height": 0}Window position from top-left corner.+#### `save_recording_path`++```python+save_recording_path: str | None = None+```++Directory path for saving video recordings.++#### `trace_path`++```python+trace_path: str | None = None+```++Directory path for saving Agent trace files. Files are automatically named as `{trace_path}/{context_id}.zip`.++----## Playwright Parameters+<a name="playwright-parameters"></a><a name="playwright"></a>-https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context+### Playwright Launch Options-All the parameters below are standard playwright parameters and can be passed to both `BrowserSession` and `BrowserProfile`.-They are defined in `browser_use/browser/profile.py`.-### Launch Settings+All the parameters below are standard playwright parameters and can be passed to both `BrowserSession` and `BrowserProfile`.+They are defined in `browser_use/browser/profile.py`. See here for the [official Playwright documentation](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context) for all of these options.#### `headless`@@ -366,7 +376,13 @@ Path to browser executable for custom installations.user_data_dir: str | Path | None = '~/.config/browseruse/profiles/default'```-Directory for browser profile data. Set to None to use an ephemeral profile aka incognito mode.+Directory for browser profile data. Set to `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`@@ -374,7 +390,7 @@ Directory for browser profile data. Set to None to use an ephemeral profile akaargs: list[str] = []```-Additional command-line arguments to pass to the browser.+Additional command-line arguments to pass to the browser. See here for the [full list of available chrome launch options](https://peter.sh/experiments/chromium-command-line-switches/).#### `ignore_default_args`@@ -382,7 +398,7 @@ Additional command-line arguments to pass to the browser.ignore_default_args: list[str] | bool = ['--enable-automation', '--disable-extensions']```-List of default CLI args to stop playwright from applying.+List of default CLI args to stop playwright from including when launching chrome. Set it to `True` to disable *all* default options (not recommended).#### `env`@@ -390,7 +406,7 @@ List of default CLI args to stop playwright from applying.env: dict[str, str] = {}```-Environment variables to set when launching browser.+Extra environment variables to set when launching browser. e.g. `{'DISPLAY': '1'}` to use a specific X11 display.#### `chromium_sandbox`@@ -398,7 +414,8 @@ Environment variables to set when launching browser.chromium_sandbox: bool = not IN_DOCKER```-Whether to enable Chromium sandboxing (recommended unless inside Docker).+Whether to enable Chromium sandboxing (recommended for security). Should always be `False` when running inside Docker+because Docker provides its own sandboxing can conflict with Chrome's.#### `devtools`@@ -406,8 +423,7 @@ Whether to enable Chromium sandboxing (recommended unless inside Docker).devtools: bool = False```--Whether to open DevTools panel automatically (only works when headless=False).+Whether to open DevTools panel automatically (only works when `headless=False`).#### `slow_mo`@@ -439,7 +455,7 @@ Whether to automatically accept all downloads.proxy: dict | None = None```-Proxy settings. Example: `{"server": "http://proxy.com:8080", "username": "user", "password": "pass"}`+Proxy settings. Example: `{"server": "http://proxy.com:8080", "username": "user", "password": "pass"}`.#### `permissions`@@ -447,7 +463,7 @@ Proxy settings. Example: `{"server": "http://proxy.com:8080", "username": "user"permissions: list[str] = ['clipboard-read', 'clipboard-write', 'notifications']```-Browser permissions to grant.+Browser permissions to grant. See here for the [full list of available permission](https://playwright.dev/python/docs/api/class-browsercontext#browser-context-grant-permissions).#### `storage_state`@@ -455,7 +471,8 @@ Browser permissions to grant.storage_state: str | Path | dict | None = None```-Browser storage state (cookies, localStorage). Can be file path or dict.+Browser storage state (cookies, localStorage). Can be file path or dict. See here for the [Playwright `storage_state` documentation](https://playwright.dev/python/docs/api/class-browsercontext#browser-context-storage-state) on how to use it.+This option is only applied when launching a new browser using the default builtin playwright chromium.@@ -480,7 +497,7 @@ default_navigation_timeout: float | None = NoneDefault timeout for page navigation in milliseconds.-### Display and Viewport Settings+### Playwright Viewport OptionsConfigure browser window size, viewport, and display properties:@@ -607,8 +624,7 @@ forced_colors: ForcedColors = 'none'Forced colors mode: `'active'`, `'none'`, `'null'`--### Security and Network Settings+### Playwright Security Options> See `allowed_domains` above too!@@ -693,23 +709,9 @@ client_certificates: list[ClientCertificate] = []Client certificates to be used with requests.-### Recording and Debugging--#### `save_recording_path`--```python-save_recording_path: str | None = None-```--Directory path for saving video recordings.--#### `trace_path`+### Playwright Recording Options-```python-trace_path: str | None = None-```--Directory path for saving trace files. Files are automatically named as `{trace_path}/{context_id}.zip`.+Note: Browser Use also provides some of our own recording-related options not listed below (see above).#### `record_video_dir`@@ -717,12 +719,13 @@ Directory path for saving trace files. Files are automatically named as `{trace_record_video_dir: str | Path | None = None```-Directory to save video recordings.+Directory to save video recordings. [Playwright Docs: `record_video_dir`](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context-option-record-video-dir)#### `record_video_size````python-record_video_size: dict | None = None+record_video_size: dict | None = None. [Playwright Docs: `record_video_size`](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context-option-record-video-size)+```Video size. Example: `{"width": 1280, "height": 720}`@@ -819,11 +822,9 @@ BrowserProfile(Playwright provides launch & context args to [emulate common device fingerprints](https://playwright.dev/python/docs/emulation).`BrowserSession` and `BrowserProfile` take all the standard playwright args so we are able to support these as well.------## Example+## Full Example```pythonfrom browser_use import BrowserSession, BrowserProfile, Agent@@ -838,11 +839,12 @@ browser_profile = BrowserProfile(highlight_elements=True,viewport_expansion=500,allowed_domains=['*.google.com', 'http*://*.wikipedia.org'],+ user_data_dir=None,)browser_session = BrowserSession(- headless=True,browser_profile=browser_profile,+ headless=True, # extra kwargs to the session override the defaults in the profile)# you can drive a session without the agent / reuse it between agents@@ -859,19 +861,18 @@ async def run_search():)```-## Summary-- **BrowserSession** parameters (defined in `browser_use/browser/session.py`) handle connection and runtime state-- **BrowserProfile** parameters (defined in `browser_use/browser/profile.py`) handle all playwright configuration+- **BrowserSession** (defined in `browser_use/browser/session.py`) handles the live browser connection and runtime state+- **BrowserProfile** (defined in `browser_use/browser/profile.py`) can be used to store a collection of static configuration-`BrowserProfile` is a flat collection of config, it's consumed by these calls depending on how we need to connect/launch:+Configuration parameters defined on either are consumed by these calls depending on whether we're connecting/launching:- `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(...)`-- BrowserUse custom settings+- Browser Use's own internal methodsFor more details on Playwright's browser context options, see the [official documentation](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context).diff --git a/docs/customize/real-browser.mdx b/docs/customize/real-browser.mdxindex f7350c6e2..3df3fee45 100644--- a/docs/customize/real-browser.mdx+++ b/docs/customize/real-browser.mdx@@ -26,8 +26,8 @@ browser_session = BrowserSession(# For Windows: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'# For Linux: '/usr/bin/google-chrome'- # Use a specific data directory on disk (optional)- user_data_dir='~/.config/browseruse/profiles/default',+ # Use a specific data directory on disk (optional, set to None for incognito)+ user_data_dir='~/.config/browseruse/profiles/default', # this is the default# ... any other BrowserProfile or playwright launch_persistnet_context config...# headless=False,)