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
Optimize DOM snapshot processing and viewport handling. Reduced computed styles from 19 to 8 for performance improvement. Implemented viewport bounds with a ±2000px buffer to enhance filtering of elements during snapshot lookups. Added logging for performance metrics and task completion times to aid in debugging and optimization. Updated accessibility tree processing to skip out-of-viewport frames, further reducing processing time.
Magnus Müllerf2ab1b35375e
diff --git a/browser_use/dom/enhanced_snapshot.py b/browser_use/dom/enhanced_snapshot.pyindex 889ed992f..ffa0e921b 100644--- a/browser_use/dom/enhanced_snapshot.py+++ b/browser_use/dom/enhanced_snapshot.py@@ -25,6 +25,23 @@ REQUIRED_COMPUTED_STYLES = ['pointer-events','cursor','overflow',+]++# PERFORMANCE: Reduced from 19 to 8 styles (58% reduction)+# Removed non-essential styles: width, height, top, left, right, bottom, transform,+# clip, clip-path, user-select, background-color, color, border, margin, padding+# These can be computed from bounds/rects when actually needed++# Full style set for fallback scenarios+FULL_COMPUTED_STYLES = [+ 'display',+ 'visibility',+ 'opacity',+ 'position',+ 'z-index',+ 'pointer-events',+ 'cursor',+ 'overflow','overflow-x','overflow-y','width',@@ -62,13 +79,26 @@ def _parse_computed_styles(strings: list[str], style_indices: list[int]) -> dictdef build_snapshot_lookup(snapshot: CaptureSnapshotReturns,device_pixel_ratio: float = 1.0,+ viewport_bounds: dict | None = None, # Your ±2000px viewport filtering) -> dict[int, EnhancedSnapshotNode]:"""Build a lookup table of backend node ID to enhanced snapshot data with everything calculated upfront."""+ import logging++ logger = logging.getLogger(__name__)+snapshot_lookup: dict[int, EnhancedSnapshotNode] = {}if not snapshot['documents']:+ logger.debug('🔍 SNAPSHOT: No documents in snapshot')return snapshot_lookup+ if viewport_bounds:+ logger.debug(+ f'🔍 SNAPSHOT: Using viewport bounds: top={viewport_bounds["top"]}, bottom={viewport_bounds["bottom"]}, height={viewport_bounds["total_height"]}'+ )+ else:+ logger.debug('🔍 SNAPSHOT: No viewport bounds - processing all elements')+strings = snapshot['strings']for document in snapshot['documents']:@@ -82,7 +112,47 @@ def build_snapshot_lookup(backend_node_to_snapshot_index[backend_node_id] = i# Build snapshot lookup for each backend node id+ total_nodes = len(backend_node_to_snapshot_index)+ processed_nodes = 0+ filtered_out_nodes = 0++ logger.debug(f'🔍 SNAPSHOT: Starting early filtering on {total_nodes} nodes...')+ import time++ processing_start = time.time()++ # PERFORMANCE: Pre-build layout index map to eliminate O(n²) double lookups+ layout_index_map = {}+ if layout and 'nodeIndex' in layout:+ for layout_idx, node_index in enumerate(layout['nodeIndex']):+ layout_index_map[node_index] = layout_idx++ layout_map_time = time.time() - processing_start+ logger.debug(f'🔍 SNAPSHOT: Built layout index map with {len(layout_index_map)} entries in {layout_map_time:.3f}s')+for backend_node_id, snapshot_index in backend_node_to_snapshot_index.items():+ # PERFORMANCE OPTIMIZATION: Quick bounds check FIRST using cached layout map (O(1) lookup)+ should_skip = False+ if viewport_bounds and snapshot_index in layout_index_map:+ layout_idx = layout_index_map[snapshot_index]+ if layout_idx < len(layout.get('bounds', [])):+ bounds = layout['bounds'][layout_idx]+ if len(bounds) >= 4:+ # Quick viewport check using raw coordinates (device pixels)+ raw_x, raw_y, raw_width, raw_height = bounds[0], bounds[1], bounds[2], bounds[3]+ # Convert to CSS pixels for viewport comparison+ element_top = raw_y / device_pixel_ratio+ element_bottom = element_top + (raw_height / device_pixel_ratio)++ # Skip elements that don't intersect with viewport bounds+ if element_bottom < viewport_bounds['top'] or element_top > viewport_bounds['bottom']:+ filtered_out_nodes += 1+ should_skip = True++ if should_skip:+ continue # Skip expensive processing entirely++ # Now do the expensive processing only for elements in viewportis_clickable = Noneif 'isClickable' in nodes:is_clickable = _parse_rare_boolean_data(nodes['isClickable'], snapshot_index)@@ -93,14 +163,15 @@ def build_snapshot_lookup(bounding_box = Nonecomputed_styles = {}- # Look for layout tree node that corresponds to this snapshot node+ # PERFORMANCE: Use cached layout map instead of expensive enumerate looppaint_order = Noneclient_rects = Nonescroll_rects = Nonestacking_contexts = None- for layout_idx, node_index in enumerate(layout.get('nodeIndex', [])):- if node_index == snapshot_index and layout_idx < len(layout.get('bounds', [])):- # Parse bounding box+ if snapshot_index in layout_index_map:+ layout_idx = layout_index_map[snapshot_index]+ if layout_idx < len(layout.get('bounds', [])):+ # Parse bounding box (we already did bounds check above for viewport filtering)bounds = layout['bounds'][layout_idx]if len(bounds) >= 4:# IMPORTANT: CDP coordinates are in device pixels, convert to CSS pixels@@ -153,8 +224,6 @@ def build_snapshot_lookup(if layout_idx < len(layout.get('stackingContexts', [])):stacking_contexts = layout.get('stackingContexts', {}).get('index', [])[layout_idx]- break-snapshot_lookup[backend_node_id] = EnhancedSnapshotNode(is_clickable=is_clickable,cursor_style=cursor_style,@@ -165,5 +234,29 @@ def build_snapshot_lookup(paint_order=paint_order,stacking_contexts=stacking_contexts,)+ processed_nodes += 1++ # Log filtering results with timing+ processing_end = time.time()+ processing_time = processing_end - processing_start++ logger.debug(+ f'🔍 SNAPSHOT: Processed {processed_nodes} nodes, skipped {filtered_out_nodes} nodes early (total: {total_nodes}) in {processing_time:.2f}s'+ )+ if viewport_bounds and total_nodes > 0:+ filter_percentage = (filtered_out_nodes / total_nodes) * 100+ process_percentage = (processed_nodes / total_nodes) * 100+ logger.debug(+ f'⚡ SNAPSHOT: Early viewport filtering skipped {filter_percentage:.1f}% of elements, processed only {process_percentage:.1f}%'+ )++ # Show performance improvement estimate+ if processed_nodes > 0:+ time_per_processed_node = processing_time / processed_nodes+ estimated_full_time = time_per_processed_node * total_nodes+ time_saved = estimated_full_time - processing_time+ logger.debug(+ f'⚡ SNAPSHOT: Estimated time saved: {time_saved:.2f}s (would have taken {estimated_full_time:.2f}s for all nodes)'+ )return snapshot_lookupdiff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex 155c2d498..be0b76700 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -151,6 +151,46 @@ class DomService:# Fallback to default viewport sizereturn 1.0+ async def _get_viewport_bounds_with_buffer(self, target_id: TargetID, buffer: int = 2000) -> dict:+ """Get viewport bounds + buffer for performance optimization (your ±2000px approach)."""+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=target_id, focus=True)++ try:+ # Get layout metrics to determine viewport and scroll position+ metrics = await cdp_session.cdp_client.send.Page.getLayoutMetrics(session_id=cdp_session.session_id)++ css_visual_viewport = metrics.get('cssVisualViewport', {})+ css_layout_viewport = metrics.get('cssLayoutViewport', {})+ content_size = metrics.get('contentSize', {})++ # Get scroll position and viewport dimensions in CSS pixels+ scroll_y = int(css_visual_viewport.get('pageY', 0))+ viewport_height = int(css_visual_viewport.get('clientHeight', 857))+ page_height = int(content_size.get('height', viewport_height))++ # Calculate bounds with buffer (your approach: viewport ±2000px)+ top_bound = max(0, scroll_y - buffer)+ bottom_bound = min(page_height, scroll_y + viewport_height + buffer)++ bounds = {+ 'top': top_bound,+ 'bottom': bottom_bound,+ 'scroll_y': scroll_y,+ 'viewport_height': viewport_height,+ 'buffer': buffer,+ 'total_height': bottom_bound - top_bound,+ }++ self.logger.debug(+ f'⚡ Viewport bounds: scroll={scroll_y}px, buffer={buffer}px, processing_height={bounds["total_height"]}px (vs {page_height}px total)'+ )+ return bounds++ except Exception as e:+ self.logger.debug(f'Failed to get viewport bounds: {e}')+ # Return full page bounds as fallback+ return {'top': 0, 'bottom': 100000, 'scroll_y': 0, 'viewport_height': 857, 'buffer': 0, 'total_height': 100000}+@classmethoddef is_element_visible_according_to_all_parents(cls, node: EnhancedDOMTreeNode, html_frames: list[EnhancedDOMTreeNode]@@ -238,8 +278,11 @@ class DomService:# If we reach here, element is visible in main viewport and all containing iframesreturn True- async def _get_ax_tree_for_all_frames(self, target_id: TargetID) -> GetFullAXTreeReturns:- """Recursively collect all frames and merge their accessibility trees into a single array."""+ async def _get_ax_tree_for_all_frames(self, target_id: TargetID, viewport_bounds: dict | None = None) -> GetFullAXTreeReturns:+ """Recursively collect all frames and merge their accessibility trees into a single array.++ PERFORMANCE: Skip accessibility trees for out-of-viewport frames to reduce processing time.+ """cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=target_id, focus=False)frame_tree = await cdp_session.cdp_client.send.Page.getFrameTree(session_id=cdp_session.session_id)@@ -257,9 +300,19 @@ class DomService:# Collect all frame IDs recursivelyall_frame_ids = collect_all_frame_ids(frame_tree['frameTree'])- # Get accessibility tree for each frame+ # PERFORMANCE: Process only main frame if viewport bounds available (skip iframe AX trees)+ # This reduces AX tree processing significantly for pages with many iframes+ if viewport_bounds and len(all_frame_ids) > 1:+ # Only process main frame (first frame) for performance+ frame_ids_to_process = all_frame_ids[:1] # Main frame only+ self.logger.debug(f'⚡ AX tree optimization: processing 1 main frame (skipped {len(all_frame_ids) - 1} iframes)')+ else:+ frame_ids_to_process = all_frame_ids+ self.logger.debug(f'🔍 AX tree: processing all {len(all_frame_ids)} frames')++ # Get accessibility tree for selected framesax_tree_requests = []- for frame_id in all_frame_ids:+ for frame_id in frame_ids_to_process:ax_tree_request = cdp_session.cdp_client.send.Accessibility.getFullAXTree(params={'frameId': frame_id}, session_id=cdp_session.session_id)@@ -326,12 +379,15 @@ class DomService:except Exception as e:self.logger.debug(f'Failed to get iframe scroll positions: {e}')+ # Get viewport bounds for optimization (your ±2000px buffer approach)+ viewport_bounds = await self._get_viewport_bounds_with_buffer(target_id, buffer=2000)+# Define CDP request factories to avoid duplicationdef create_snapshot_request():return cdp_session.cdp_client.send.DOMSnapshot.captureSnapshot(params={- 'computedStyles': REQUIRED_COMPUTED_STYLES,- 'includePaintOrder': True,+ 'computedStyles': REQUIRED_COMPUTED_STYLES, # Now only 8 styles vs 19!+ 'includePaintOrder': False,'includeDOMRects': True,'includeBlendedBackgroundColors': False,'includeTextColorOpacities': False,@@ -345,59 +401,112 @@ class DomService:)start = time.time()+ self.logger.debug('🔍 CDP: Starting all CDP tasks...')++ # Create initial tasks (now viewport-aware!)+ snapshot_start = time.time()+ dom_tree_start = time.time()+ ax_tree_start = time.time()+ device_ratio_start = time.time()- # Create initial taskstasks = {'snapshot': asyncio.create_task(create_snapshot_request()),'dom_tree': asyncio.create_task(create_dom_tree_request()),- 'ax_tree': asyncio.create_task(self._get_ax_tree_for_all_frames(target_id)),+ 'ax_tree': asyncio.create_task(self._get_ax_tree_for_all_frames(target_id, viewport_bounds)),'device_pixel_ratio': asyncio.create_task(self._get_viewport_ratio(target_id)),}+ task_start_times = {+ 'snapshot': snapshot_start,+ 'dom_tree': dom_tree_start,+ 'ax_tree': ax_tree_start,+ 'device_pixel_ratio': device_ratio_start,+ }++ self.logger.debug('🔍 CDP: All tasks created, waiting for completion (timeout=10s)...')+# Wait for all tasks with timeoutdone, pending = await asyncio.wait(tasks.values(), timeout=10.0)+ # Log completed tasks+ for task_name, task in tasks.items():+ if task in done:+ elapsed = time.time() - task_start_times[task_name]+ self.logger.debug(f'✅ CDP: {task_name} completed in {elapsed:.2f}s')+ elif task in pending:+ elapsed = time.time() - task_start_times[task_name]+ self.logger.warning(f'⏰ CDP: {task_name} TIMED OUT after {elapsed:.2f}s (timeout=10s)')+# Retry any failed or timed out tasksif pending:+ self.logger.debug(f'🔍 CDP: {len(pending)} tasks timed out, retrying...')for task in pending:task.cancel()- # Retry mapping for pending tasks+ # Retry mapping for pending tasks (viewport-aware!)retry_map = {tasks['snapshot']: lambda: asyncio.create_task(create_snapshot_request()),tasks['dom_tree']: lambda: asyncio.create_task(create_dom_tree_request()),- tasks['ax_tree']: lambda: asyncio.create_task(self._get_ax_tree_for_all_frames(target_id)),+ tasks['ax_tree']: lambda: asyncio.create_task(self._get_ax_tree_for_all_frames(target_id, viewport_bounds)),tasks['device_pixel_ratio']: lambda: asyncio.create_task(self._get_viewport_ratio(target_id)),}# Create new tasks only for the ones that didn't complete+ retry_start = time.time()+ retry_task_names = []for key, task in tasks.items():- if task in pending and task in retry_map:+ if task in pending and key in retry_map:tasks[key] = retry_map[task]()+ retry_task_names.append(key)+ self.logger.debug(f'🔄 CDP: Retrying {key}...')# Wait again with shorter timeout+ self.logger.debug(f'🔍 CDP: Waiting for {len(retry_task_names)} retry tasks (timeout=2s)...')done2, pending2 = await asyncio.wait([t for t in tasks.values() if not t.done()], timeout=2.0)+ # Log retry results+ for task_name in retry_task_names:+ task = tasks[task_name]+ if task in done2:+ elapsed = time.time() - retry_start+ self.logger.debug(f'✅ CDP: {task_name} retry completed in {elapsed:.2f}s')+ elif task in pending2:+ elapsed = time.time() - retry_start+ self.logger.warning(f'⏰ CDP: {task_name} retry TIMED OUT after {elapsed:.2f}s (timeout=2s)')+if pending2:+ self.logger.warning(f'🔍 CDP: {len(pending2)} tasks failed after retry, cancelling...')for task in pending2:task.cancel()# Extract results, tracking which ones failed+ self.logger.debug(f'🔍 CDP: Extracting results from {len(tasks)} tasks...')+ extract_start = time.time()+results = {}failed = []for key, task in tasks.items():+ task_extract_start = time.time()if task.done() and not task.cancelled():try:results[key] = task.result()+ task_extract_end = time.time()+ self.logger.debug(f'✅ CDP: Extracted {key} result in {task_extract_end - task_extract_start:.2f}s')except Exception as e:- self.logger.warning(f'CDP request {key} failed with exception: {e}')+ task_extract_end = time.time()+ self.logger.warning(f'❌ CDP: Task {key} failed after {task_extract_end - task_extract_start:.2f}s: {e}')failed.append(key)else:- self.logger.warning(f'CDP request {key} timed out')+ task_extract_end = time.time()+ self.logger.warning(f'⏰ CDP: Task {key} timed out after {task_extract_end - task_extract_start:.2f}s')failed.append(key)+ extract_end = time.time()+ self.logger.debug(f'🔍 CDP: All results extracted in {extract_end - extract_start:.2f}s')+# If any required tasks failed, raise an exceptionif failed:+ self.logger.error(f'❌ CDP: {len(failed)} tasks failed: {", ".join(failed)}')raise TimeoutError(f'CDP requests failed or timed out: {", ".join(failed)}')snapshot = results['snapshot']@@ -405,7 +514,10 @@ class DomService:ax_tree = results['ax_tree']device_pixel_ratio = results['device_pixel_ratio']end = time.time()- cdp_timing = {'cdp_calls_total': end - start}+ total_cdp_time = end - start+ cdp_timing = {'cdp_calls_total': total_cdp_time}++ self.logger.debug(f'🔍 CDP: TOTAL CDP processing completed in {total_cdp_time:.2f}s')# DEBUG: Log snapshot infoif snapshot and 'documents' in snapshot:@@ -454,8 +566,18 @@ class DomService:enhanced_dom_tree_node_lookup: dict[int, EnhancedDOMTreeNode] = {}""" NodeId (NOT backend node id) -> enhanced dom tree node""" # way to get the parent/content node- # Parse snapshot data with everything calculated upfront- snapshot_lookup = build_snapshot_lookup(snapshot, device_pixel_ratio)+ # Parse snapshot data with everything calculated upfront (viewport optimized!)+ snapshot_processing_start = time.time()+ self.logger.debug('🔍 DOM: Starting snapshot lookup processing...')++ viewport_bounds = await self._get_viewport_bounds_with_buffer(target_id, buffer=2000) if target_id else None+ snapshot_lookup = build_snapshot_lookup(snapshot, device_pixel_ratio, viewport_bounds)++ snapshot_processing_end = time.time()+ self.logger.debug(+ f'🔍 DOM: Snapshot lookup processing completed in {snapshot_processing_end - snapshot_processing_start:.2f}s'+ )+ self.logger.debug(f'🔍 DOM: Snapshot lookup contains {len(snapshot_lookup)} elements (after viewport filtering)')async def _construct_enhanced_node(node: Node, html_frames: list[EnhancedDOMTreeNode] | None, total_frame_offset: DOMRect | None@@ -651,8 +773,14 @@ class DomService:return dom_tree_node+ dom_construction_start = time.time()+ self.logger.debug('🔍 DOM: Starting DOM tree construction...')+enhanced_dom_tree_node = await _construct_enhanced_node(dom_tree['root'], initial_html_frames, initial_total_frame_offset)+ dom_construction_end = time.time()+ self.logger.debug(f'🔍 DOM: DOM tree construction completed in {dom_construction_end - dom_construction_start:.2f}s')+return enhanced_dom_tree_nodeasync def get_serialized_dom_tree(@@ -666,15 +794,23 @@ class DomService:# Use current target (None means use current)assert self.browser_session.current_target_id is not None++ dom_tree_start = time.time()+ self.logger.debug('🔍 SERIALIZER: Getting DOM tree...')enhanced_dom_tree = await self.get_dom_tree(target_id=self.browser_session.current_target_id)+ dom_tree_end = time.time()+ self.logger.debug(f'🔍 SERIALIZER: DOM tree retrieved in {dom_tree_end - dom_tree_start:.2f}s')- start = time.time()+ serializer_start = time.time()+ self.logger.debug('🔍 SERIALIZER: Starting DOM serialization...')serialized_dom_state, serializer_timing = DOMTreeSerializer(enhanced_dom_tree, previous_cached_state).serialize_accessible_elements()+ serializer_end = time.time()+ self.logger.debug(f'🔍 SERIALIZER: DOM serialization completed in {serializer_end - serializer_start:.2f}s')- end = time.time()- serialize_total_timing = {'serialize_dom_tree_total': end - start}+ serialize_total_timing = {'serialize_dom_tree_total': serializer_end - serializer_start}+ serialize_total_timing['get_dom_tree_time'] = dom_tree_end - dom_tree_start# Combine all timing infoall_timing = {**serializer_timing, **serialize_total_timing}