Skip to content

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

fix: identify cloud documents across execution contexts

Gregor Žuničb2a50588dea6
diff --git a/browser_use/integrations/anthropic/browser_use.py b/browser_use/integrations/anthropic/browser_use.py
index 8eaa3c00e..17a1d7098 100644
--- a/browser_use/integrations/anthropic/browser_use.py
+++ b/browser_use/integrations/anthropic/browser_use.py
@@ -11,7 +11,6 @@ import base64
import contextlib
import json
import re
-import secrets
import time
from collections import defaultdict, deque
from collections.abc import Callable
@@ -89,8 +88,6 @@ class BrowserUse(BetaAsyncAbstractBrowserToolset20260801):
self._ref_names: dict[Reference, str] = {}
self._next_ref = 1
self._documents: dict[str, str] = {}
- self._next_document = 1
- self._document_property = f'__browser_use_document_{secrets.token_hex(16)}'
self._reported_tabs: set[str] = set()
self._last_tabs: list[dict] = []
self._observer: CDPClient | None = None
@@ -418,27 +415,15 @@ class BrowserUse(BetaAsyncAbstractBrowserToolset20260801):
async def _document(self, page):
# Prime DOM.resolveNode for this attached session. Cloud transports may
# remap CDP document, frame, loader, and session IDs between calls while the
- # page is unchanged. A non-enumerable marker in the page's main world stays
- # stable across those remaps and disappears when a new document is created.
+ # page is unchanged. The navigation time origin belongs to the document,
+ # stays stable across execution contexts, and changes on a real navigation.
session_id = await page.session_id
await self._cdp.send.DOM.getDocument(params={'depth': 0}, session_id=session_id)
tab = page._target_id
- candidate = f'{tab}:{self._next_document}'
- self._next_document += 1
- key = json.dumps(self._document_property)
- value = json.dumps(candidate)
- document = await self._eval(
- page,
- f"""(() => {{
- const key = {key};
- if (!Object.prototype.hasOwnProperty.call(globalThis, key)) {{
- Object.defineProperty(globalThis, key, {{value: {value}, configurable: false}});
- }}
- return globalThis[key];
- }})()""",
- )
- if not isinstance(document, str):
+ time_origin = await self._eval(page, 'performance.timeOrigin')
+ if not isinstance(time_origin, (int, float)):
raise ToolError('Unable to identify the current browser document.')
+ document = f'{tab}:{time_origin}'
if self._documents.get(tab) != document:
self._invalidate_refs(tab)
self._documents[tab] = document
diff --git a/tests/ci/integrations/test_anthropic_browser_use.py b/tests/ci/integrations/test_anthropic_browser_use.py
index 5627da4cd..767ae44ca 100644
--- a/tests/ci/integrations/test_anthropic_browser_use.py
+++ b/tests/ci/integrations/test_anthropic_browser_use.py
@@ -139,7 +139,7 @@ async def test_reference_document_identity_survives_cdp_session_rotation() -> No
driver = BrowserUse()
driver.browser = SimpleNamespace(cdp_client=FakeCDP())
- current_document = 'document-a'
+ current_document = 1000.5
async def fake_eval(page, expression):
return current_document
@@ -153,7 +153,7 @@ async def test_reference_document_identity_survives_cdp_session_rotation() -> No
assert await driver._document(page) == document
assert ref in driver._refs
- current_document = 'document-b'
+ current_document = 2000.5
assert await driver._document(page) != document
assert ref not in driver._refs