Skip to content

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

refactor: remove target type caching and streamline target retrieval in session management

reformedotbff596f2e7b8
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index ef59bdf71..610885514 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -467,9 +467,6 @@ class BrowserSession(BaseModel):
await self.session_manager.clear()
self.session_manager = None
- # Note: _targets, _sessions, _target_sessions are now owned by SessionManager
- # They're cleared via SessionManager.clear() above
-
self._cdp_client_root = None # type: ignore
self._cached_browser_state_summary = None
self._cached_selector_map.clear()
@@ -1183,7 +1180,8 @@ class BrowserSession(BaseModel):
# CRITICAL: Only allow focus change to 'page' type targets, not iframes/workers
if focus and self.agent_focus_target_id != target_id:
# Get target type from SessionManager
- target_type = self.session_manager._target_types.get(target_id, 'unknown')
+ target = self.session_manager.get_target(target_id)
+ target_type = target.target_type if target else 'unknown'
if target_type == 'page':
self.logger.debug(f'[SessionManager] Switching focus: {self.agent_focus_target_id[:8]}... → {target_id[:8]}...')
@@ -1205,17 +1203,6 @@ class BrowserSession(BaseModel):
return session
- @property
- def current_target_id(self) -> str | None:
- return self.agent_focus_target_id
-
- @property
- def current_session_id(self) -> str | None:
- if not self.agent_focus_target_id or not self.session_manager:
- return None
- session = self.session_manager.get_session_for_target(self.agent_focus_target_id)
- return session.session_id if session else None
-
# endregion - ========== CDP-based ... ==========
# region - ========== Helper Methods ==========
@@ -1865,14 +1852,12 @@ class BrowserSession(BaseModel):
# Search in SessionManager targets (exact match first)
for target_id, target in self.session_manager.get_all_targets().items():
- target_type = self.session_manager._target_types.get(target_id, 'unknown')
- if target_type in ('page', 'tab') and target.url == url:
+ if target.target_type in ('page', 'tab') and target.url == url:
return target_id
# Still not found, try substring match as fallback
for target_id, target in self.session_manager.get_all_targets().items():
- target_type = self.session_manager._target_types.get(target_id, 'unknown')
- if target_type in ('page', 'tab') and url in target.url:
+ if target.target_type in ('page', 'tab') and url in target.url:
return target_id
raise ValueError(f'No TargetID found for url={url}')
@@ -2599,12 +2584,10 @@ class BrowserSession(BaseModel):
# Build TargetInfo dicts from SessionManager owned data (crystal clear ownership)
result = []
for target_id, target in self.session_manager.get_all_targets().items():
- target_type = self.session_manager._target_types.get(target_id, 'unknown')
-
# Create TargetInfo dict
target_info: TargetInfo = {
'targetId': target.target_id,
- 'type': target_type,
+ 'type': target.target_type,
'title': target.title,
'url': target.url,
'attached': True,
@@ -2835,7 +2818,7 @@ class BrowserSession(BaseModel):
async def _cdp_navigate(self, url: str, target_id: TargetID | None = None) -> None:
"""Navigate to URL using CDP Page.navigate."""
- # Use provided target_id or fall back to current_target_id
+ # Use provided target_id or fall back to agent_focus_target_id
assert self._cdp_client_root is not None, 'CDP client not initialized - browser may not be connected yet'
assert self.agent_focus_target_id is not None, 'Agent focus not initialized - browser may not be connected yet'
diff --git a/browser_use/browser/session_manager.py b/browser_use/browser/session_manager.py
index 5f35233d9..ce8b5818c 100644
--- a/browser_use/browser/session_manager.py
+++ b/browser_use/browser/session_manager.py
@@ -45,9 +45,6 @@ class SessionManager:
# Reverse mapping: session -> target it belongs to
self._session_to_target: dict[SessionID, TargetID] = {}
- # Target -> type cache (page, iframe, worker, etc.) - types are immutable
- self._target_types: dict[TargetID, str] = {}
-
self._lock = asyncio.Lock()
self._recovery_lock = asyncio.Lock()
@@ -128,9 +125,8 @@ class SessionManager:
List of Target objects for all page/tab targets
"""
page_targets = []
- for target_id, target in self._targets.items():
- target_type = self._target_types.get(target_id, 'unknown')
- if target_type in ('page', 'tab'):
+ for target in self._targets.values():
+ if target.target_type in ('page', 'tab'):
page_targets.append(target)
return page_targets
@@ -155,7 +151,6 @@ class SessionManager:
self._sessions.clear()
self._target_sessions.clear()
self._session_to_target.clear()
- self._target_types.clear()
self.logger.info('[SessionManager] Cleared all owned data (targets, sessions, mappings)')
@@ -308,10 +303,6 @@ class SessionManager:
self._target_sessions[target_id].add(session_id)
self._session_to_target[session_id] = target_id
- # Cache target type (immutable, set once)
- if target_id not in self._target_types:
- self._target_types[target_id] = target_type
-
# Create or update Target (source of truth for url/title)
if target_id not in self._targets:
from browser_use.browser.session import Target
@@ -440,12 +431,9 @@ class SessionManager:
f'session={session_id[:8]}... (target was already removed or attach event was missed)'
)
- # Get target type before cleaning up cache (needed for TabClosedEvent dispatch)
- target_type = self._target_types.get(target_id)
-
- # Clean up target type cache if target fully removed
- if target_id not in self._target_sessions and target_id in self._target_types:
- del self._target_types[target_id]
+ # Get target type before cleaning up (needed for TabClosedEvent dispatch)
+ target = self._targets.get(target_id)
+ target_type = target.target_type if target else None
# Remove session from owned sessions dict
if session_id in self._sessions:
@@ -630,7 +618,8 @@ class SessionManager:
for tid in target_ids_to_wait_for:
session = self.get_session_for_target(tid)
if session:
- target_type = self._target_types.get(tid, 'unknown')
+ target = self._targets.get(tid)
+ target_type = target.target_type if target else 'unknown'
# For pages, verify monitoring is enabled
if target_type in ('page', 'tab'):
if hasattr(session, '_lifecycle_events') and session._lifecycle_events is not None:
@@ -659,7 +648,8 @@ class SessionManager:
for tid in target_ids_to_wait_for:
session = self.get_session_for_target(tid)
if session:
- target_type = self._target_types.get(tid, 'unknown')
+ target = self._targets.get(tid)
+ target_type = target.target_type if target else 'unknown'
# For pages, verify monitoring is enabled
if target_type in ('page', 'tab'):
if hasattr(session, '_lifecycle_events') and session._lifecycle_events is not None:
diff --git a/browser_use/browser/watchdogs/crash_watchdog.py b/browser_use/browser/watchdogs/crash_watchdog.py
index 62b7521f3..eeb4eeed7 100644
--- a/browser_use/browser/watchdogs/crash_watchdog.py
+++ b/browser_use/browser/watchdogs/crash_watchdog.py
@@ -113,11 +113,9 @@ class CrashWatchdog(BaseWatchdog):
# Track that we've added listeners to this target
self._targets_with_listeners.add(target_id)
- # Get target info for logging
- targets = await cdp_session.cdp_client.send.Target.getTargets()
- target_info = next((t for t in targets['targetInfos'] if t['targetId'] == target_id), None)
- if target_info:
- self.logger.debug(f'[CrashWatchdog] Added target to monitoring: {target_info.get("url", "unknown")}')
+ target = self.browser_session.session_manager.get_target(target_id)
+ if target:
+ self.logger.debug(f'[CrashWatchdog] Added target to monitoring: {target.url}')
except Exception as e:
self.logger.warning(f'[CrashWatchdog] Failed to attach to target {target_id}: {e}')
@@ -164,22 +162,16 @@ class CrashWatchdog(BaseWatchdog):
"""Handle target crash detected via CDP."""
self.logger.debug(f'[CrashWatchdog] Target crashed: {target_id[:8]}..., waiting for detach event')
- # Get target info for logging
- 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)
+ target = self.browser_session.session_manager.get_target(target_id)
is_agent_focus = (
- target_info
+ target
and self.browser_session.agent_focus_target_id
- and target_info['targetId'] == self.browser_session.agent_focus_target_id
+ and target.target_id == self.browser_session.agent_focus_target_id
)
- if is_agent_focus and target_info:
- self.logger.error(
- f'[CrashWatchdog] 💥 Agent focus tab crashed: {target_info.get("url", "unknown")} '
- f'(SessionManager will auto-recover)'
- )
+ if is_agent_focus:
+ self.logger.error(f'[CrashWatchdog] 💥 Agent focus tab crashed: {target.url} (SessionManager will auto-recover)')
# Emit browser error event
self.event_bus.dispatch(
@@ -187,7 +179,7 @@ class CrashWatchdog(BaseWatchdog):
error_type='TargetCrash',
message=f'Target crashed: {target_id}',
details={
- 'url': target_info.get('url') if target_info else None,
+ 'url': target.url if target else None,
'target_id': target_id,
'was_agent_focus': is_agent_focus,
},
@@ -293,16 +285,13 @@ class CrashWatchdog(BaseWatchdog):
self.logger.debug(f'[CrashWatchdog] Checking browser health for target {self.browser_session.agent_focus_target_id}')
cdp_session = await self.browser_session.get_or_create_cdp_session()
- for target in (await self.browser_session.cdp_client.send.Target.getTargets()).get('targetInfos', []):
- if target.get('type') == 'page':
- cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=target.get('targetId'))
- if self._is_new_tab_page(target.get('url')) and target.get('url') != 'about:blank':
- self.logger.debug(
- f'[CrashWatchdog] Redirecting chrome://new-tab-page/ to about:blank {target.get("url")}'
- )
- await cdp_session.cdp_client.send.Page.navigate(
- params={'url': 'about:blank'}, session_id=cdp_session.session_id
- )
+ for target in self.browser_session.session_manager.get_all_page_targets():
+ if self._is_new_tab_page(target.url) and target.url != 'about:blank':
+ self.logger.debug(f'[CrashWatchdog] Redirecting chrome://new-tab-page/ to about:blank {target.url}')
+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=target.target_id)
+ await cdp_session.cdp_client.send.Page.navigate(
+ params={'url': 'about:blank'}, session_id=cdp_session.session_id
+ )
# Quick ping to check if session is alive
self.logger.debug(f'[CrashWatchdog] Attempting to run simple JS test expression in session {cdp_session} 1+1')
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.py
index 67e24fa8a..849f3b601 100644
--- a/browser_use/browser/watchdogs/default_action_watchdog.py
+++ b/browser_use/browser/watchdogs/default_action_watchdog.py
@@ -1765,14 +1765,12 @@ class DefaultActionWatchdog(BaseWatchdog):
if element_node.frame_id:
# Element is in an iframe, need to get session for that frame
try:
- # Get all targets
- targets = await self.browser_session.cdp_client.send.Target.getTargets()
+ all_targets = self.browser_session.session_manager.get_all_targets()
# Find the target for this frame
- for target in targets['targetInfos']:
- if target['type'] == 'iframe' and element_node.frame_id in str(target.get('targetId', '')):
+ for target_id, target in all_targets.items():
+ if target.target_type == 'iframe' and element_node.frame_id in str(target_id):
# Create temporary session for iframe target without switching focus
- target_id = target['targetId']
temp_session = await self.browser_session.get_or_create_cdp_session(target_id, focus=False)
return temp_session.session_id
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index bf71a3907..2b7365c7d 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -534,6 +534,9 @@ class DomService:
height=snapshot_data.bounds.height,
)
+ session = self.browser_session.session_manager.get_session_for_target(target_id)
+ session_id = session.session_id if session else None
+
dom_tree_node = EnhancedDOMTreeNode(
node_id=node['nodeId'],
backend_node_id=node['backendNodeId'],
@@ -543,7 +546,7 @@ class DomService:
attributes=attributes or {},
is_scrollable=node.get('isScrollable', None),
frame_id=node.get('frameId', None),
- session_id=self.browser_session.current_session_id,
+ session_id=session_id,
target_id=target_id,
content_document=None,
shadow_root_type=shadow_root_type,
@@ -690,11 +693,15 @@ class DomService:
frame_info = all_frames.get(frame_id)
iframe_document_target = None
if frame_info and frame_info.get('frameTargetId'):
- # Get the target info for this iframe
- targets = await self.browser_session.cdp_client.send.Target.getTargets()
- iframe_document_target = next(
- (t for t in targets['targetInfos'] if t['targetId'] == frame_info['frameTargetId']), None
- )
+ iframe_target_id = frame_info['frameTargetId']
+ iframe_target = self.browser_session.session_manager.get_target(iframe_target_id)
+ if iframe_target:
+ iframe_document_target = {
+ 'targetId': iframe_target.target_id,
+ 'url': iframe_target.url,
+ 'title': iframe_target.title,
+ 'type': iframe_target.target_type,
+ }
else:
iframe_document_target = None
# if target actually exists in one of the frames, just recursively build the dom tree for it
@@ -703,7 +710,7 @@ class DomService:
f'Getting content document for iframe {node.get("frameId", None)} at depth {iframe_depth + 1}'
)
content_document, _ = await self.get_dom_tree(
- target_id=iframe_document_target.get('targetId'),
+ target_id=iframe_document_target['targetId'],
all_frames=all_frames,
# TODO: experiment with this values -> not sure whether the whole cross origin iframe should be ALWAYS included as soon as some part of it is visible or not.
# Current config: if the cross origin iframe is AT ALL visible, then just include everything inside of it!
@@ -759,12 +766,12 @@ class DomService:
start_total = time.time()
# Use current target (None means use current)
- assert self.browser_session.current_target_id is not None
+ assert self.browser_session.agent_focus_target_id is not None
# Build DOM tree (includes CDP calls for snapshot, DOM, AX tree)
# Note: all_frames is fetched lazily inside get_dom_tree only if cross-origin iframes need it
enhanced_dom_tree, dom_tree_timing = await self.get_dom_tree(
- target_id=self.browser_session.current_target_id,
+ target_id=self.browser_session.agent_focus_target_id,
all_frames=None, # Lazy - will fetch if needed
)
diff --git a/browser_use/tools/registry/service.py b/browser_use/tools/registry/service.py
index f70ae6f2d..f1608ff8c 100644
--- a/browser_use/tools/registry/service.py
+++ b/browser_use/tools/registry/service.py
@@ -333,14 +333,12 @@ class Registry(Generic[Context]):
if sensitive_data:
# Get current URL if browser_session is provided
current_url = None
- if browser_session and browser_session.current_target_id:
+ if browser_session and browser_session.agent_focus_target_id:
try:
- # Get current page info using CDP
- targets = await browser_session.cdp_client.send.Target.getTargets()
- for target in targets.get('targetInfos', []):
- if target.get('targetId') == browser_session.current_target_id:
- current_url = target.get('url')
- break
+ # Get current page info from session_manager
+ target = browser_session.session_manager.get_target(browser_session.agent_focus_target_id)
+ if target:
+ current_url = target.url
except Exception:
pass
validated_params = self._replace_sensitive_data(validated_params, sensitive_data, current_url)
diff --git a/tests/scripts/test_frame_hierarchy.py b/tests/scripts/test_frame_hierarchy.py
index 3d3ea148b..4105cbfdc 100755
--- a/tests/scripts/test_frame_hierarchy.py
+++ b/tests/scripts/test_frame_hierarchy.py
@@ -27,13 +27,12 @@ async def analyze_frame_hierarchy(url):
print('FRAME HIERARCHY ANALYSIS')
print('=' * 80)
- # Get all targets
- targets = await session.cdp_client.send.Target.getTargets()
- all_targets = targets.get('targetInfos', [])
+ # Get all targets from SessionManager
+ all_targets = session.session_manager.get_all_targets()
# Separate by type
- page_targets = [t for t in all_targets if t.get('type') == 'page']
- iframe_targets = [t for t in all_targets if t.get('type') == 'iframe']
+ page_targets = [target for target in all_targets.values() if target.target_type == 'page']
+ iframe_targets = [target for target in all_targets.values() if target.target_type == 'iframe']
print('\n📊 Target Summary:')
print(f' Total targets: {len(all_targets)}')
@@ -42,26 +41,25 @@ async def analyze_frame_hierarchy(url):
# Show all targets
print('\n📋 All Targets:')
- for i, target in enumerate(all_targets):
- t_type = target.get('type')
- t_url = target.get('url', 'none')
- t_id = target.get('targetId', 'unknown')
+ for i, (target_id, target) in enumerate(all_targets.items()):
+ if target.target_type in ['page', 'iframe']:
+ print(f'\n [{i + 1}] Type: {target.target_type}')
+ print(f' URL: {target.url}')
+ print(f' Target ID: {target.target_id[:30]}...')
- if t_type in ['page', 'iframe']:
- print(f'\n [{i + 1}] Type: {t_type}')
- print(f' URL: {t_url}')
- print(f' Target ID: {t_id[:30]}...')
- print(f' Attached: {target.get("attached", False)}')
+ # Check if target has active sessions
+ has_session = session.session_manager.get_session_for_target(target.target_id) is not None
+ print(f' Has Session: {has_session}')
# Get main page frame tree
- main_target = next((t for t in page_targets if url in t.get('url', '')), page_targets[0] if page_targets else None)
+ main_target = next((t for t in page_targets if url in t.url), page_targets[0] if page_targets else None)
if main_target:
print('\n📐 Main Page Frame Tree:')
- print(f' Target: {main_target["url"]}')
- print(f' Target ID: {main_target["targetId"][:30]}...')
+ print(f' Target: {main_target.url}')
+ print(f' Target ID: {main_target.target_id[:30]}...')
- s = await session.cdp_client.send.Target.attachToTarget(params={'targetId': main_target['targetId'], 'flatten': True})
+ s = await session.cdp_client.send.Target.attachToTarget(params={'targetId': main_target.target_id, 'flatten': True})
sid = s['sessionId']
try:
@@ -101,11 +99,11 @@ async def analyze_frame_hierarchy(url):
print('\n🔸 OOPIF Target Frame Trees:')
for iframe_target in iframe_targets:
- print(f'\n OOPIF Target: {iframe_target["url"]}')
- print(f' Target ID: {iframe_target["targetId"][:30]}...')
+ print(f'\n OOPIF Target: {iframe_target.url}')
+ print(f' Target ID: {iframe_target.target_id[:30]}...')
s = await session.cdp_client.send.Target.attachToTarget(
- params={'targetId': iframe_target['targetId'], 'flatten': True}
+ params={'targetId': iframe_target.target_id, 'flatten': True}
)
sid = s['sessionId']
@@ -173,7 +171,7 @@ async def analyze_frame_hierarchy(url):
print(' ❌ PROBLEM: Cross-origin frame incorrectly marked as root!')
elif not parent and url != 'about:blank' and url not in ['chrome://newtab/', 'about:blank']:
# Check if this should be the main frame
- if any(url in t.get('url', '') for t in page_targets):
+ if any(url in t.url for t in page_targets):
print(' ✅ Correctly identified as root frame')
if is_cross: