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
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.pyindex 8eaa3c00e..17a1d7098 100644--- a/browser_use/integrations/anthropic/browser_use.py+++ b/browser_use/integrations/anthropic/browser_use.py@@ -11,7 +11,6 @@ import base64import contextlibimport jsonimport re-import secretsimport timefrom collections import defaultdict, dequefrom collections.abc import Callable@@ -89,8 +88,6 @@ class BrowserUse(BetaAsyncAbstractBrowserToolset20260801):self._ref_names: dict[Reference, str] = {}self._next_ref = 1self._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_idawait 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] = documentdiff --git a/tests/ci/integrations/test_anthropic_browser_use.py b/tests/ci/integrations/test_anthropic_browser_use.pyindex 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() -> Nodriver = BrowserUse()driver.browser = SimpleNamespace(cdp_client=FakeCDP())- current_document = 'document-a'+ current_document = 1000.5async def fake_eval(page, expression):return current_document@@ -153,7 +153,7 @@ async def test_reference_document_identity_survives_cdp_session_rotation() -> Noassert await driver._document(page) == documentassert ref in driver._refs- current_document = 'document-b'+ current_document = 2000.5assert await driver._document(page) != documentassert ref not in driver._refs