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
Handle same-document navigations without burning the readiness timeout
Saurav Pandab4b6868232a8
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex e4933a7ed..c22c487a3 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -1062,6 +1062,15 @@ class BrowserSession(BaseModel):return Nonenavigation_id = nav_result.get('loaderId')++ # Page.navigate omits loaderId for same-document navigations (#fragment,+ # History API): the navigation is already committed and Chrome emits no new+ # load/DOMContentLoaded lifecycle events for it — waiting would only burn+ # the timeout against stale events from the previous document load.+ if not navigation_id:+ duration_ms = (asyncio.get_event_loop().time() - nav_start_time) * 1000+ self.logger.debug(f'✅ Page ready for {url} (same-document navigation, {duration_ms:.0f}ms)')+ return Nonestart_time = asyncio.get_event_loop().time()seen_events = []@@ -1094,7 +1103,7 @@ class BrowserSession(BaseModel):# Defense for events without a usable loaderId: only trust them if# they arrived after this navigation started.- if event_data.get('timestamp', 0) < nav_start_time and (not event_loader_id or not navigation_id):+ if not event_loader_id and event_data.get('timestamp', 0) < nav_start_time:continueif event_name in acceptable_events:diff --git a/tests/ci/browser/test_navigation_readiness.py b/tests/ci/browser/test_navigation_readiness.pyindex 7fb6ed0a6..414b9a4b5 100644--- a/tests/ci/browser/test_navigation_readiness.py+++ b/tests/ci/browser/test_navigation_readiness.py@@ -110,3 +110,30 @@ async def test_successful_navigation_returns_no_timeout_status(httpserver, browsstatus = await browser_session._navigate_and_wait(httpserver.url_for('/ok'), target_id, wait_until='load')assert status is None+++async def test_same_document_navigation_completes_immediately(httpserver, browser_session: BrowserSession):+ """Fragment/History-API navigations must not burn the readiness timeout.++ Page.navigate returns no loaderId for same-document navigations and Chrome+ emits no new load/DOMContentLoaded lifecycle events for them — the navigation+ is already committed when Page.navigate returns.+ """+ httpserver.expect_request('/page').respond_with_data(+ '<html><body><a id="anchor" name="section">s</a></body></html>', content_type='text/html'+ )++ await browser_session.navigate_to(httpserver.url_for('/page'))+ target_id = browser_session.agent_focus_target_id+ assert target_id is not None++ # Let the first load's trailing lifecycle events (networkIdle fires ~1s after+ # load) drain, so a stale event can't accidentally satisfy the fragment wait.+ await asyncio.sleep(1.5)++ start = time.monotonic()+ status = await browser_session._navigate_and_wait(httpserver.url_for('/page') + '#section', target_id, wait_until='load')+ elapsed = time.monotonic() - start++ assert elapsed < FAST_NAVIGATION_BOUND_S, f'same-document navigation took {elapsed:.2f}s — burned the readiness timeout'+ assert status is None, f'same-document navigation reported a bogus timeout: {status!r}'