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
chore: added timing logs
reformedote64edf82d624
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 27fb8de0e..59180d98f 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -815,7 +815,11 @@ class BrowserSession(BaseModel):NO handler registration here - handlers are registered ONCE per session in SessionManager.We poll stored events instead to avoid handler accumulation."""+ # Performance tracking+ t0 = asyncio.get_event_loop().time()+cdp_session = await self.get_or_create_cdp_session(target_id, focus=False)+ t1 = asyncio.get_event_loop().time()if timeout is None:current_url = cdp_session.url@@ -826,14 +830,15 @@ class BrowserSession(BaseModel):)timeout = 2.0 if same_domain else 4.0- # Start performance tracking+ # Start navigation CDP callnav_start_time = asyncio.get_event_loop().time()+ t2 = asyncio.get_event_loop().time()- # Start navigation firstnav_result = await cdp_session.cdp_client.send.Page.navigate(params={'url': url, 'transitionType': 'address_bar'},session_id=cdp_session.session_id,)+ t3 = asyncio.get_event_loop().time()# Check for immediate navigation errorsif nav_result.get('errorText'):@@ -874,13 +879,21 @@ class BrowserSession(BaseModel):continueif event_name == 'networkIdle':- duration_ms = (asyncio.get_event_loop().time() - nav_start_time) * 1000- self.logger.debug(f'✅ Page ready for {url} (networkIdle, {duration_ms:.0f}ms)')+ t4 = asyncio.get_event_loop().time()+ duration_ms = (t4 - t0) * 1000+ self.logger.info(+ f'✅ Page ready for {url} (networkIdle, {duration_ms:.0f}ms) '+ f'[session:{(t1 - t0) * 1000:.0f}ms, cdp:{(t3 - t2) * 1000:.0f}ms, wait:{(t4 - t3) * 1000:.0f}ms]'+ )returnelif event_name == 'load':- duration_ms = (asyncio.get_event_loop().time() - nav_start_time) * 1000- self.logger.debug(f'✅ Page ready for {url} (load, {duration_ms:.0f}ms)')+ t4 = asyncio.get_event_loop().time()+ duration_ms = (t4 - t0) * 1000+ self.logger.info(+ f'✅ Page ready for {url} (load, {duration_ms:.0f}ms) '+ f'[session:{(t1 - t0) * 1000:.0f}ms, cdp:{(t3 - t2) * 1000:.0f}ms, wait:{(t4 - t3) * 1000:.0f}ms]'+ )returnexcept Exception as e:@@ -890,14 +903,19 @@ class BrowserSession(BaseModel):await asyncio.sleep(poll_interval)# Timeout - continue anyway with detailed diagnostics- duration_ms = (asyncio.get_event_loop().time() - nav_start_time) * 1000+ t4 = asyncio.get_event_loop().time()+ duration_ms = (t4 - t0) * 1000if not seen_events:self.logger.error(f'❌ No lifecycle events received for {url} after {duration_ms:.0f}ms! '- f'Monitoring may have failed. Target: {cdp_session.target_id[:8]}'+ f'Monitoring may have failed. Target: {cdp_session.target_id[:8]} '+ f'[session:{(t1 - t0) * 1000:.0f}ms, cdp:{(t3 - t2) * 1000:.0f}ms, wait:{(t4 - t3) * 1000:.0f}ms]')else:- self.logger.warning(f'⚠️ Page readiness timeout ({timeout}s, {duration_ms:.0f}ms) for {url}')+ self.logger.warning(+ f'⚠️ Page readiness timeout ({timeout}s, {duration_ms:.0f}ms) for {url} '+ f'[session:{(t1 - t0) * 1000:.0f}ms, cdp:{(t3 - t2) * 1000:.0f}ms, wait:{(t4 - t3) * 1000:.0f}ms]'+ )async def on_SwitchTabEvent(self, event: SwitchTabEvent) -> TargetID:"""Handle tab switching - core browser functionality."""@@ -987,55 +1005,31 @@ class BrowserSession(BaseModel):async def on_AgentFocusChangedEvent(self, event: AgentFocusChangedEvent) -> None:"""Handle agent focus change - update focus and clear cache."""+ t0 = asyncio.get_event_loop().time()self.logger.debug(f'🔄 AgentFocusChangedEvent received: target_id=...{event.target_id[-4:]} url={event.url}')# Clear cached DOM state since focus changed- # self.logger.debug('🔄 Clearing DOM cache...')if self._dom_watchdog:self._dom_watchdog.clear_cache()- # self.logger.debug('🔄 Cleared DOM cache after focus change')# Clear cached browser state- # self.logger.debug('🔄 Clearing cached browser state...')self._cached_browser_state_summary = Noneself._cached_selector_map.clear()- self.logger.debug('🔄 Cached browser state cleared')- all_targets = await self._cdp_get_all_pages(include_chrome=True)+ t1 = asyncio.get_event_loop().time()# Update agent focus if a specific target_id is providedif event.target_id:self.agent_focus = await self.get_or_create_cdp_session(target_id=event.target_id, focus=True)- self.logger.debug(f'🔄 Updated agent focus to tab target_id=...{event.target_id[-4:]}')+ t2 = asyncio.get_event_loop().time()else:raise RuntimeError('AgentFocusChangedEvent received with no target_id for newly focused tab')- # Test that the browser is responsive by evaluating a simple expression- if self.agent_focus:- self.logger.debug('🔄 Testing tab responsiveness...')- try:- test_result = await asyncio.wait_for(- self.agent_focus.cdp_client.send.Runtime.evaluate(- params={'expression': '1 + 1', 'returnByValue': True}, session_id=self.agent_focus.session_id- ),- timeout=2.0,- )- if test_result.get('result', {}).get('value') == 2:- # self.logger.debug('🔄 ✅ Browser is responsive after focus change')- pass- else:- raise Exception('❌ Failed to execute test JS expression with Page.evaluate')- except Exception as e:- self.logger.error(f'🔄 ❌ Target {self.agent_focus.target_id} seems closed/crashed, switching to fallback')- # Get last page from SessionManager- page_sessions = self._session_manager.get_all_page_sessions()- last_target_id = page_sessions[-1].target_id if page_sessions else None- self.agent_focus = await self.get_or_create_cdp_session(target_id=last_target_id, focus=True)- raise+ # Note: Responsiveness check removed - if lifecycle events fired, page is responsive+ # No need to test with Runtime.evaluate() - that's redundant and adds 80-150ms latency# Dispatch NavigationCompleteEvent when tab focus changes# This ensures PDF detection and downloads work when switching tabsif event.target_id and event.url:- self.logger.debug(f'🔄 Dispatching NavigationCompleteEvent for tab switch to {event.url[:50]}...')await self.event_bus.dispatch(NavigationCompleteEvent(target_id=event.target_id,@@ -1043,6 +1037,13 @@ class BrowserSession(BaseModel):))+ t3 = asyncio.get_event_loop().time()+ total_ms = (t3 - t0) * 1000+ self.logger.info(+ f'🔄 AgentFocusChangedEvent completed ({total_ms:.0f}ms) '+ f'[clear:{(t1 - t0) * 1000:.0f}ms, session:{(t2 - t1) * 1000:.0f}ms, events:{(t3 - t2) * 1000:.0f}ms]'+ )+# self.logger.debug('🔄 AgentFocusChangedEvent handler completed successfully')async def on_FileDownloadedEvent(self, event: FileDownloadedEvent) -> None:diff --git a/browser_use/browser/watchdogs/downloads_watchdog.py b/browser_use/browser/watchdogs/downloads_watchdog.pyindex d44f51ff0..66d5322a0 100644--- a/browser_use/browser/watchdogs/downloads_watchdog.py+++ b/browser_use/browser/watchdogs/downloads_watchdog.py@@ -135,6 +135,7 @@ class DownloadsWatchdog(BaseWatchdog):async def on_NavigationCompleteEvent(self, event: NavigationCompleteEvent) -> None:"""Check for PDFs after navigation completes."""+ t0 = asyncio.get_event_loop().time()self.logger.debug(f'[DownloadsWatchdog] NavigationCompleteEvent received for {event.url}, tab #{event.target_id[-4:]}')# Clear PDF cache for the navigated URL since content may have changed@@ -151,12 +152,24 @@ class DownloadsWatchdog(BaseWatchdog):target_id = event.target_idself.logger.debug(f'[DownloadsWatchdog] Got target_id={target_id} for tab #{event.target_id[-4:]}')+ t1 = asyncio.get_event_loop().time()is_pdf = await self.check_for_pdf_viewer(target_id)+ t2 = asyncio.get_event_loop().time()+if is_pdf:self.logger.debug(f'[DownloadsWatchdog] 📄 PDF detected at {event.url}, triggering auto-download...')download_path = await self.trigger_pdf_download(target_id)+ t3 = asyncio.get_event_loop().time()if not download_path:self.logger.warning(f'[DownloadsWatchdog] ⚠️ PDF download failed for {event.url}')+ else:+ t3 = t2++ total_ms = (t3 - t0) * 1000+ self.logger.info(+ f'[DownloadsWatchdog] NavigationCompleteEvent completed ({total_ms:.0f}ms) '+ f'[check_pdf:{(t2 - t1) * 1000:.0f}ms, download:{(t3 - t2) * 1000:.0f}ms]'+ )def _is_auto_download_enabled(self) -> bool:"""Check if auto-download PDFs is enabled in browser profile."""diff --git a/browser_use/browser/watchdogs/security_watchdog.py b/browser_use/browser/watchdogs/security_watchdog.pyindex 343ab947e..e1ce08788 100644--- a/browser_use/browser/watchdogs/security_watchdog.py+++ b/browser_use/browser/watchdogs/security_watchdog.py@@ -49,6 +49,9 @@ class SecurityWatchdog(BaseWatchdog):async def on_NavigationCompleteEvent(self, event: NavigationCompleteEvent) -> None:"""Check if navigated URL is allowed (catches redirects to blocked domains)."""+ import asyncio+ t0 = asyncio.get_event_loop().time()+# Check if the navigated URL is allowed (in case of redirects)if not self._is_url_allowed(event.url):self.logger.warning(f'⛔️ Navigation to non-allowed URL detected: {event.url}')@@ -70,6 +73,9 @@ class SecurityWatchdog(BaseWatchdog):except Exception as e:passself.logger.error(f'⛔️ Failed to navigate to about:blank: {type(e).__name__} {e}')++ total_ms = (asyncio.get_event_loop().time() - t0) * 1000+ self.logger.info(f'[SecurityWatchdog] NavigationCompleteEvent completed ({total_ms:.0f}ms)')async def on_TabCreatedEvent(self, event: TabCreatedEvent) -> None:"""Check if new tab URL is allowed."""