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
Bridge BrowserProfile storage state
Magnus Müller95aaff6b3f9c
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex ac0c0f819..e9b92b070 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -176,10 +176,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai- The terminal core applies viewport metrics with CDP `Emulation.setDeviceMetricsOverride`, and the headless screenshot helper reuses those settings instead of forcing its default 1280x720 viewport.- Proof: `test_rust_agent_translates_browser_profile_viewport`, `browser_viewport_env_parses_device_metrics`, and `browser_viewport_env_honors_no_viewport`.+34. BrowserProfile storage state bridge+ - `BrowserProfile.storage_state` dicts and JSON paths are serialized into `BU_BROWSER_STORAGE_STATE`.+ - The terminal core applies compatible cookies with CDP `Storage.setCookies` and installs origin-scoped localStorage/sessionStorage init scripts with `Page.addScriptToEvaluateOnNewDocument`.+ - Proof: `test_rust_agent_translates_browser_profile_storage_state` and `browser_storage_state_env_parses_cookies_and_storage_scripts`.+## 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` (43 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (44 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`@@ -187,6 +192,7 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_permissions_env_parses_json_array -- --nocapture`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_download_behavior_env_ -- --nocapture`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_viewport_env_ -- --nocapture`+- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_storage_state_env_parses_cookies_and_storage_scripts -- --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.pyindex 1ada47e97..6a64da53a 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -318,6 +318,35 @@ def _extract_browser_viewport(return no_viewport, viewport+def _storage_state_value(value: Any) -> dict[str, Any] | None:+ if isinstance(value, dict):+ return value+ if hasattr(value, 'model_dump'):+ dumped = value.model_dump()+ return dumped if isinstance(dumped, dict) else None+ if isinstance(value, (str, os.PathLike)) and str(value):+ path = Path(value).expanduser()+ if not path.exists():+ return None+ try:+ loaded = json.loads(path.read_text())+ except (OSError, json.JSONDecodeError):+ return None+ return loaded if isinstance(loaded, dict) else None+ return None+++def _extract_browser_storage_state(+ browser_session: BrowserSession | None, browser_profile: BrowserProfile | None+) -> dict[str, Any] | None:+ session_profile = getattr(browser_session, 'browser_profile', None)+ for profile in (session_profile, browser_profile, browser_session):+ value = _storage_state_value(getattr(profile, 'storage_state', None))+ if value is not None:+ return value+ return None++def _is_managed_browser_mode(mode: str) -> bool:normalized = mode.strip().lower().replace('_', '-').replace(' ', '-')return normalized in {'managed-headless', 'headless', 'headless-chromium', 'managed-headed', 'managed', 'headed'}@@ -855,6 +884,7 @@ class Agent(Generic[AgentStructuredOutput]):self.browser_no_viewport, self.browser_viewport = _extract_browser_viewport(self.browser_session, self.browser_profile)+ self.browser_storage_state = _extract_browser_storage_state(self.browser_session, self.browser_profile)self.sensitive_data_context = _sensitive_data_context(sensitive_data)self.display_files_in_done_text = display_files_in_done_textself.file_system_path = file_system_path@@ -1310,6 +1340,8 @@ class Agent(Generic[AgentStructuredOutput]):env['BU_BROWSER_NO_VIEWPORT'] = 'true' if self.browser_no_viewport else 'false'if self.browser_viewport:env['BU_BROWSER_VIEWPORT'] = json.dumps(self.browser_viewport)+ if self.browser_storage_state:+ env['BU_BROWSER_STORAGE_STATE'] = json.dumps(self.browser_storage_state)if self.managed_browser_env and _is_managed_browser_mode(browser_mode):env.update(self.managed_browser_env)if self.managed_browser_args and _is_managed_browser_mode(browser_mode):diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex e3fd6d2be..a94afd684 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -423,6 +423,42 @@ def test_rust_agent_translates_browser_profile_viewport(monkeypatch):assert 'BU_BROWSER_VIEWPORT' not in no_viewport_env+def test_rust_agent_translates_browser_profile_storage_state(monkeypatch, tmp_path):+ from browser_use.rust import Agent++ profile_storage_state_path = tmp_path / 'storage_state.json'+ storage_state = {+ 'cookies': [+ {+ 'name': 'sid',+ 'value': 'secret',+ 'domain': '.example.com',+ 'path': '/',+ }+ ],+ 'origins': [+ {+ 'origin': 'https://example.com',+ 'localStorage': [{'name': 'theme', 'value': 'dark'}],+ }+ ],+ }+ profile_storage_state_path.write_text(json.dumps(storage_state))++ class BrowserProfile:+ storage_state = profile_storage_state_path+ cdp_url = 'http://127.0.0.1:9222'++ 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_storage_state == storage_state+ assert json.loads(env['BU_BROWSER_STORAGE_STATE']) == storage_state++def test_rust_agent_translates_browser_profile_user_data_dir(monkeypatch, tmp_path):from browser_use.rust import Agent