Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Bridge BrowserProfile user agent

Magnus Müllerb662f83aa200
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index e6f31fba8..e18ab95f7 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -191,10 +191,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- The terminal core now supports configurable highlight duration in addition to its existing highlight enable and color controls.
- Proof: `test_rust_agent_translates_browser_profile_highlights` and `browser_highlight_env_controls_color_and_duration`.
+37. BrowserProfile user agent CDP bridge
+ - `BrowserProfile.user_agent` is serialized into `BU_BROWSER_USER_AGENT`.
+ - The terminal core applies it with CDP `Network.setUserAgentOverride`, covering remote-CDP and cloud browsers in addition to the existing managed Chromium `--user-agent` launch arg.
+ - Proof: `test_rust_agent_translates_browser_profile_remote_user_agent`, `test_rust_agent_translates_browser_profile_managed_launch_args`, and `browser_user_agent_env_builds_override_params`.
+
## Current Verification
- `python3 -m py_compile browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`
-- `uv run pytest -q tests/ci/test_rust_agent.py` (46 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (47 tests)
- `cargo build -q -p browser-use-cli`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent bare_browser_connect_resolves_to_selected_managed_mode_with_launch_args -- --nocapture`
@@ -205,6 +210,7 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_storage_state_env_parses_cookies_and_storage_scripts -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser cdp_headers_env_builds_websocket_request_headers -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_highlight_env_controls_color_and_duration -- --nocapture`
+- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_user_agent_env_builds_override_params -- --nocapture`
- Managed-headless end-to-end:
- `BROWSER_USE_TERMINAL_BINARY=/home/exedev/Developer/terminal/target/debug/browser-use-terminal BROWSER_USE_RUST_BROWSER_MODE=managed-headless BU_TASK='Open https://example.com and report the page title only.' BU_MAX_STEPS=12 timeout 300 uv run python examples/rust_agent/basic.py`
- Output: `Example Domain`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 66666bf45..f8f61de01 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -243,6 +243,15 @@ def _extract_cdp_headers(browser_session: BrowserSession | None, browser_profile
return headers
+def _extract_user_agent(browser_session: BrowserSession | None, browser_profile: BrowserProfile | None) -> str | None:
+ session_profile = getattr(browser_session, 'browser_profile', None)
+ for profile in (session_profile, browser_profile, browser_session):
+ value = getattr(profile, 'user_agent', None)
+ if isinstance(value, str) and value:
+ return value
+ return None
+
+
def _extract_highlight_settings(
browser_session: BrowserSession | None, browser_profile: BrowserProfile | None
) -> tuple[bool | None, str | None, int | None]:
@@ -918,6 +927,7 @@ class Agent(Generic[AgentStructuredOutput]):
self.managed_browser_executable_path = _managed_browser_executable_path(self.browser_session, self.browser_profile)
self.managed_browser_env = _managed_browser_env(self.browser_session, self.browser_profile)
self.cdp_headers = _extract_cdp_headers(self.browser_session, self.browser_profile)
+ self.browser_user_agent = _extract_user_agent(self.browser_session, self.browser_profile)
self.highlight_enabled, self.highlight_color, self.highlight_duration_ms = _extract_highlight_settings(
self.browser_session, self.browser_profile
)
@@ -1376,6 +1386,8 @@ class Agent(Generic[AgentStructuredOutput]):
env['BU_CDP_URL'] = cdp_url
if self.cdp_headers:
env['BU_CDP_HEADERS'] = json.dumps(self.cdp_headers)
+ if self.browser_user_agent:
+ env['BU_BROWSER_USER_AGENT'] = self.browser_user_agent
if self.highlight_enabled is not None:
env['BROWSER_USE_TERMINAL_AUTO_HIGHLIGHT'] = 'true' if self.highlight_enabled else 'false'
if self.highlight_color:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 17f4325ed..f25c9666c 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -201,6 +201,23 @@ def test_rust_agent_translates_browser_profile_cdp_headers(monkeypatch):
assert json.loads(env['BU_CDP_HEADERS']) == agent.cdp_headers
+def test_rust_agent_translates_browser_profile_remote_user_agent(monkeypatch):
+ from browser_use.rust import Agent
+
+ class BrowserProfile:
+ cdp_url = 'http://127.0.0.1:9222'
+ user_agent = 'BrowserUseRemote/2.0'
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+
+ agent = Agent(task='report title', browser_profile=BrowserProfile())
+ env = agent._run_env()
+
+ assert env['LLM_BROWSER_BROWSER_MODE'] == 'remote-cdp'
+ assert agent.browser_user_agent == 'BrowserUseRemote/2.0'
+ assert env['BU_BROWSER_USER_AGENT'] == 'BrowserUseRemote/2.0'
+
+
def test_rust_agent_translates_browser_profile_highlights(monkeypatch):
from browser_use.rust import Agent
@@ -311,6 +328,8 @@ def test_rust_agent_translates_browser_profile_managed_launch_args(monkeypatch):
assert env['LLM_BROWSER_BROWSER_MODE'] == 'managed-headless'
assert agent.managed_browser_args == launch_args
+ assert agent.browser_user_agent == 'BrowserUseTest/1.0'
+ assert env['BU_BROWSER_USER_AGENT'] == 'BrowserUseTest/1.0'
assert '--lang=en-US' in launch_args
assert '--window-size=1440,900' in launch_args
assert '--proxy-server=http://proxy.example:8080' in launch_args