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
Refactor DOM snapshot processing by removing viewport bounds handling and optimizing logging. Updated snapshot lookup to process all elements without early filtering, enhancing clarity and performance metrics. Adjusted computed styles handling for consistency.
Magnus Müllerd0685dc970f0
diff --git a/browser_use/dom/enhanced_snapshot.py b/browser_use/dom/enhanced_snapshot.pyindex 5f2f07350..1320332bf 100644--- a/browser_use/dom/enhanced_snapshot.py+++ b/browser_use/dom/enhanced_snapshot.py@@ -94,7 +94,6 @@ 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@@ -107,13 +106,6 @@ def build_snapshot_lookup(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']:@@ -129,9 +121,8 @@ def build_snapshot_lookup(# Build snapshot lookup for each backend node idtotal_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...')+ logger.debug(f'🔍 SNAPSHOT: Starting processing {total_nodes} nodes...')import timeprocessing_start = time.time()@@ -146,28 +137,6 @@ def build_snapshot_lookup(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)@@ -251,27 +220,10 @@ def build_snapshot_lookup()processed_nodes += 1- # Log filtering results with timing+ # Log results with timingprocessing_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)'- )+ logger.debug(f'🔍 SNAPSHOT: Processed {processed_nodes} nodes (total: {total_nodes}) in {processing_time:.2f}s')return snapshot_lookupdiff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex 346be6072..4f4bb836c 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -151,46 +151,6 @@ 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]@@ -370,15 +330,12 @@ 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, # Now only 8 styles vs 19!- 'includePaintOrder': False,+ 'computedStyles': REQUIRED_COMPUTED_STYLES,+ 'includePaintOrder': True,'includeDOMRects': True,'includeBlendedBackgroundColors': False,'includeTextColorOpacities': False,@@ -394,7 +351,7 @@ class DomService:start = time.time()self.logger.debug('🔍 CDP: Starting all CDP tasks...')- # Create initial tasks (now viewport-aware!)+ # Create initial taskssnapshot_start = time.time()dom_tree_start = time.time()ax_tree_start = time.time()@@ -434,7 +391,7 @@ class DomService:for task in pending:task.cancel()- # Retry mapping for pending tasks (viewport-aware!)+ # Retry mapping for pending tasksretry_map = {tasks['snapshot']: lambda: asyncio.create_task(create_snapshot_request()),tasks['dom_tree']: lambda: asyncio.create_task(create_dom_tree_request()),@@ -557,18 +514,17 @@ 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 (viewport optimized!)+ # Parse snapshot data with everything calculated upfront (O(1) hash map 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_lookup = build_snapshot_lookup(snapshot, device_pixel_ratio)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)')+ self.logger.debug(f'🔍 DOM: Snapshot lookup contains {len(snapshot_lookup)} elements')async def _construct_enhanced_node(node: Node, html_frames: list[EnhancedDOMTreeNode] | None, total_frame_offset: DOMRect | None