Skip to content

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

refactor: optimize session handling and logging

reformedotbcd88c21c2f5
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 59180d98f..d9fc6b8ec 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -1024,27 +1024,13 @@ class BrowserSession(BaseModel):
else:
raise RuntimeError('AgentFocusChangedEvent received with no target_id for newly focused tab')
- # 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 tabs
- if event.target_id and event.url:
- await self.event_bus.dispatch(
- NavigationCompleteEvent(
- target_id=event.target_id,
- url=event.url,
- )
- )
-
- t3 = asyncio.get_event_loop().time()
- total_ms = (t3 - t0) * 1000
+ total_ms = (t2 - 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]'
+ f'[clear:{(t1 - t0) * 1000:.0f}ms, session:{(t2 - t1) * 1000:.0f}ms]'
)
- # self.logger.debug('πŸ”„ AgentFocusChangedEvent handler completed successfully')
+ # self.logger.debug('πŸ”„ AgentFocusChangedEvent handler completed successfully')
async def on_FileDownloadedEvent(self, event: FileDownloadedEvent) -> None:
"""Track downloaded files during this session."""
@@ -1276,10 +1262,8 @@ class BrowserSession(BaseModel):
# Update focus if requested
# CRITICAL: Only allow focus change to 'page' type targets, not iframes/workers
if focus and self.agent_focus.target_id != target_id:
- # Check target type before allowing focus change
- targets = await self._cdp_client_root.send.Target.getTargets()
- target_info = next((t for t in targets['targetInfos'] if t['targetId'] == target_id), None)
- target_type = target_info.get('type') if target_info else 'unknown'
+ # Get target type from SessionManager
+ target_type = self._session_manager._target_types.get(target_id, 'unknown')
if target_type == 'page':
self.logger.debug(f'[SessionManager] Switching focus: {self.agent_focus.target_id[:8]}... β†’ {target_id[:8]}...')
diff --git a/browser_use/browser/watchdogs/downloads_watchdog.py b/browser_use/browser/watchdogs/downloads_watchdog.py
index 66d5322a0..9ff2da616 100644
--- a/browser_use/browser/watchdogs/downloads_watchdog.py
+++ b/browser_use/browser/watchdogs/downloads_watchdog.py
@@ -956,15 +956,13 @@ class DownloadsWatchdog(BaseWatchdog):
"""
self.logger.debug(f'[DownloadsWatchdog] Checking if target {target_id} is PDF viewer...')
- # Get target info to get URL
- cdp_client = self.browser_session.cdp_client
- targets = await cdp_client.send.Target.getTargets()
- target_info = next((t for t in targets['targetInfos'] if t['targetId'] == target_id), None)
- if not target_info:
- self.logger.warning(f'[DownloadsWatchdog] No target info found for {target_id}')
+ # Get URL from session (zero CDP calls!)
+ session = await self.browser_session._session_manager.get_session_for_target(target_id)
+ if not session:
+ self.logger.warning(f'[DownloadsWatchdog] No session found for {target_id}')
return False
- page_url = target_info.get('url', '')
+ page_url = session.url
# Check cache first
if page_url in self._pdf_viewer_cache: