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: enhance DOM tree construction and timing metrics
reformedot97420238c11f
diff --git a/browser_use/actor/page.py b/browser_use/actor/page.pyindex e76da29fc..59f59a73b 100644--- a/browser_use/actor/page.py+++ b/browser_use/actor/page.py@@ -405,7 +405,9 @@ class Page:dom_service = self.dom_service- enhanced_dom_tree = await dom_service.get_dom_tree(target_id=self._target_id)+ # Fetch all_frames for DOM tree construction+ all_frames, _ = await self._browser_session.get_all_frames()+ enhanced_dom_tree, _ = await dom_service.get_dom_tree(target_id=self._target_id, all_frames=all_frames)serialized_dom_state, _ = DOMTreeSerializer(enhanced_dom_tree, None, paint_order_filtering=Truediff --git a/browser_use/browser/watchdogs/dom_watchdog.py b/browser_use/browser/watchdogs/dom_watchdog.pyindex ca42f7e51..fc539220d 100644--- a/browser_use/browser/watchdogs/dom_watchdog.py+++ b/browser_use/browser/watchdogs/dom_watchdog.py@@ -284,7 +284,8 @@ class DOMWatchdog(BaseWatchdog):self.logger.debug('π DOMWatchdog.on_BrowserStateRequestEvent: β³ Waiting for page stability...')try:if pending_requests_before_wait:- await asyncio.sleep(1)+ # Reduced from 1s to 0.3s for faster DOM builds while still allowing critical resources to load+ await asyncio.sleep(0.3)self.logger.debug('π DOMWatchdog.on_BrowserStateRequestEvent: β Page stability complete')except Exception as e:self.logger.warning(@@ -397,34 +398,6 @@ class DOMWatchdog(BaseWatchdog):self.logger.warning(f'π DOMWatchdog.on_BrowserStateRequestEvent: Clean screenshot failed: {e}')screenshot_b64 = None- # Apply Python-based highlighting if both DOM and screenshot are available- # COMMENTED OUT: Removes highlight numbers from screenshots for code-use mode- if (- False- and screenshot_b64- and content- and content.selector_map- and self.browser_session.browser_profile.highlight_elements- ):- try:- self.logger.debug('π DOMWatchdog.on_BrowserStateRequestEvent: π¨ Applying Python-based highlighting...')- from browser_use.browser.python_highlights import create_highlighted_screenshot_async-- # Get CDP session for viewport info- cdp_session = await self.browser_session.get_or_create_cdp_session()- start = time.time()- screenshot_b64 = await create_highlighted_screenshot_async(- screenshot_b64,- content.selector_map,- cdp_session,- self.browser_session.browser_profile.filter_highlight_ids,- )- self.logger.debug(- f'π DOMWatchdog.on_BrowserStateRequestEvent: β Applied highlights to {len(content.selector_map)} elements in {time.time() - start:.2f}s'- )- except Exception as e:- self.logger.warning(f'π DOMWatchdog.on_BrowserStateRequestEvent: Python highlighting failed: {e}')-# Add browser-side highlights for user visibilityif content and content.selector_map and self.browser_session.browser_profile.dom_highlight_elements:try:@@ -575,12 +548,22 @@ class DOMWatchdog(BaseWatchdog):previous_cached_state=previous_state,)end = time.time()+ total_time_ms = (end - start) * 1000self.logger.debug('π DOMWatchdog._build_dom_tree_without_highlights: β DomService.get_serialized_dom_tree completed')- self.logger.debug(f'Time taken to get DOM tree: {end - start} seconds')- self.logger.debug(f'Timing breakdown: {timing_info}')+ # Calculate sum of all tracked timings+ tracked_time_ms = sum(timing_info.values())+ untracked_time_ms = total_time_ms - tracked_time_ms++ # Format timing values to 2 decimal places+ timing_formatted = {k: round(v, 2) for k, v in timing_info.items()}++ self.logger.debug(f'β±οΈ Total DOM tree time: {total_time_ms:.2f}ms')+ self.logger.debug(f'π Timing breakdown (all in ms): {timing_formatted}')+ if untracked_time_ms > 1.0: # Only log if significant+ self.logger.debug(f'β οΈ Untracked time: {untracked_time_ms:.2f}ms')# Update selector map for other watchdogsself.logger.debug('π DOMWatchdog._build_dom_tree_without_highlights: Updating selector maps...')@@ -642,25 +625,6 @@ class DOMWatchdog(BaseWatchdog):self.logger.warning(f'πΈ Clean screenshot failed: {type(e).__name__}: {e}')raise- async def _wait_for_stable_network(self):- """Wait for page stability - simplified for CDP-only branch."""- start_time = time.time()-- # Apply minimum wait time first (let page settle)- min_wait = self.browser_session.browser_profile.minimum_wait_page_load_time- if min_wait > 0:- self.logger.debug(f'β³ Minimum wait: {min_wait}s')- await asyncio.sleep(min_wait)-- # Apply network idle wait time (for dynamic content like iframes)- network_idle_wait = self.browser_session.browser_profile.wait_for_network_idle_page_load_time- if network_idle_wait > 0:- self.logger.debug(f'β³ Network idle wait: {network_idle_wait}s')- await asyncio.sleep(network_idle_wait)-- elapsed = time.time() - start_time- self.logger.debug(f'β Page stability wait completed in {elapsed:.2f}s')-def _detect_pagination_buttons(self, selector_map: dict[int, EnhancedDOMTreeNode]) -> list['PaginationButton']:"""Detect pagination buttons from the DOM selector map.diff --git a/browser_use/dom/markdown_extractor.py b/browser_use/dom/markdown_extractor.pyindex 1389b19e2..fb44e0a23 100644--- a/browser_use/dom/markdown_extractor.py+++ b/browser_use/dom/markdown_extractor.py@@ -49,7 +49,9 @@ async def extract_clean_markdown(method = 'enhanced_dom_tree'elif dom_service is not None and target_id is not None:# DOM service path (page actor)- enhanced_dom_tree = await dom_service.get_dom_tree(target_id=target_id)+ # Fetch all_frames for DOM tree construction+ all_frames, _ = await dom_service.browser_session.get_all_frames()+ enhanced_dom_tree, _ = await dom_service.get_dom_tree(target_id=target_id, all_frames=all_frames)current_url = None # Not available via DOM servicemethod = 'dom_service'else:diff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex 96cc597ce..ac2b88e29 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -14,7 +14,6 @@ from browser_use.dom.enhanced_snapshot import ()from browser_use.dom.serializer.serializer import DOMTreeSerializerfrom browser_use.dom.views import (- CurrentPageTargets,DOMRect,EnhancedAXNode,EnhancedAXProperty,@@ -64,49 +63,6 @@ class DomService:async def __aexit__(self, exc_type, exc_value, traceback):pass # no need to cleanup anything, browser_session auto handles cleaning up session cache- async def _get_targets_for_page(self, target_id: TargetID | None = None) -> CurrentPageTargets:- """Get the target info for a specific page.-- Args:- target_id: The target ID to get info for. If None, uses current_target_id.- """- targets = await self.browser_session.cdp_client.send.Target.getTargets()-- # Use provided target_id or fall back to current_target_id- if target_id is None:- target_id = self.browser_session.current_target_id- if not target_id:- raise ValueError('No current target ID set in browser session')-- # Find main page target by ID- main_target = next((t for t in targets['targetInfos'] if t['targetId'] == target_id), None)-- if not main_target:- raise ValueError(f'No target found for target ID: {target_id}')-- # Get all frames using the new method to find iframe targets for this page- all_frames, _ = await self.browser_session.get_all_frames()-- # Find iframe targets that are children of this target- iframe_targets = []- for frame_info in all_frames.values():- # Check if this frame is a cross-origin iframe with its own target- if frame_info.get('isCrossOrigin') and frame_info.get('frameTargetId'):- # Check if this frame belongs to our target- parent_target = frame_info.get('parentTargetId', frame_info.get('frameTargetId'))- if parent_target == target_id:- # Find the target info for this iframe- iframe_target = next(- (t for t in targets['targetInfos'] if t['targetId'] == frame_info['frameTargetId']), None- )- if iframe_target:- iframe_targets.append(iframe_target)-- return CurrentPageTargets(- page_session=main_target,- iframe_sessions=iframe_targets,- )-def _build_enhanced_ax_node(self, ax_node: AXNode) -> EnhancedAXNode:properties: list[EnhancedAXProperty] | None = Noneif 'properties' in ax_node and ax_node['properties']:@@ -451,38 +407,55 @@ class DomService:async def get_dom_tree(self,target_id: TargetID,+ all_frames: dict,initial_html_frames: list[EnhancedDOMTreeNode] | None = None,initial_total_frame_offset: DOMRect | None = None,iframe_depth: int = 0,- ) -> EnhancedDOMTreeNode:+ ) -> tuple[EnhancedDOMTreeNode, dict[str, float]]:"""Get the DOM tree for a specific target.Args:target_id: Target ID of the page to get the DOM tree for.+ all_frames: Pre-fetched frame hierarchy to avoid redundant CDP calls (required)initial_html_frames: List of HTML frame nodes encountered so farinitial_total_frame_offset: Accumulated coordinate offsetiframe_depth: Current depth of iframe nesting to prevent infinite recursion++ Returns:+ Tuple of (enhanced_dom_tree_node, timing_info)"""+ timing_info: dict[str, float] = {}+ # Get all trees from CDP (snapshot, DOM, AX, viewport ratio)+ start_cdp = time.time()trees = await self._get_all_trees(target_id)+ timing_info['cdp_calls_ms'] = (time.time() - start_cdp) * 1000dom_tree = trees.dom_treeax_tree = trees.ax_treesnapshot = trees.snapshotdevice_pixel_ratio = trees.device_pixel_ratio+ # Build AX tree lookup+ start_ax_lookup = time.time()ax_tree_lookup: dict[int, AXNode] = {ax_node['backendDOMNodeId']: ax_node for ax_node in ax_tree['nodes'] if 'backendDOMNodeId' in ax_node}+ timing_info['build_ax_lookup_ms'] = (time.time() - start_ax_lookup) * 1000enhanced_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+ start_snapshot_lookup = time.time()snapshot_lookup = build_snapshot_lookup(snapshot, device_pixel_ratio)+ timing_info['build_snapshot_lookup_ms'] = (time.time() - start_snapshot_lookup) * 1000async def _construct_enhanced_node(- node: Node, html_frames: list[EnhancedDOMTreeNode] | None, total_frame_offset: DOMRect | None+ node: Node,+ html_frames: list[EnhancedDOMTreeNode] | None,+ total_frame_offset: DOMRect | None,+ all_frames: dict,) -> EnhancedDOMTreeNode:"""Recursively construct enhanced DOM tree nodes.@@ -490,7 +463,8 @@ class DomService:Args:node: The DOM node to constructhtml_frames: List of HTML frame nodes encountered so far- accumulated_iframe_offset: Accumulated coordinate translation from parent iframes (includes scroll corrections)+ total_frame_offset: Accumulated coordinate translation from parent iframes (includes scroll corrections)+ all_frames: Pre-fetched frame hierarchy to avoid redundant CDP calls"""# Initialize lists if not provided@@ -597,7 +571,7 @@ class DomService:if 'contentDocument' in node and node['contentDocument']:dom_tree_node.content_document = await _construct_enhanced_node(- node['contentDocument'], updated_html_frames, total_frame_offset+ node['contentDocument'], updated_html_frames, total_frame_offset, all_frames)dom_tree_node.content_document.parent_node = dom_tree_node# forcefully set the parent node to the content document node (helps traverse the tree)@@ -605,7 +579,9 @@ class DomService:if 'shadowRoots' in node and node['shadowRoots']:dom_tree_node.shadow_roots = []for shadow_root in node['shadowRoots']:- shadow_root_node = await _construct_enhanced_node(shadow_root, updated_html_frames, total_frame_offset)+ shadow_root_node = await _construct_enhanced_node(+ shadow_root, updated_html_frames, total_frame_offset, all_frames+ )# forcefully set the parent node to the shadow root node (helps traverse the tree)shadow_root_node.parent_node = dom_tree_nodedom_tree_node.shadow_roots.append(shadow_root_node)@@ -623,7 +599,7 @@ class DomService:if child['nodeId'] in shadow_root_node_ids:continuedom_tree_node.children_nodes.append(- await _construct_enhanced_node(child, updated_html_frames, total_frame_offset)+ await _construct_enhanced_node(child, updated_html_frames, total_frame_offset, all_frames))# Set visibility using the collected HTML frames@@ -684,10 +660,9 @@ class DomService:self.logger.debug('Skipping invisible cross-origin iframe')if should_process_iframe:- # Use get_all_frames to find the iframe's target+ # Use pre-fetched all_frames to find the iframe's target (no redundant CDP call)frame_id = node.get('frameId', None)if frame_id:- all_frames, _ = await self.browser_session.get_all_frames()frame_info = all_frames.get(frame_id)iframe_document_target = Noneif frame_info and frame_info.get('frameTargetId'):@@ -703,8 +678,9 @@ class DomService:self.logger.debug(f'Getting content document for iframe {node.get("frameId", None)} at depth {iframe_depth + 1}')- content_document = await self.get_dom_tree(+ content_document, _ = await self.get_dom_tree(target_id=iframe_document_target.get('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!# initial_html_frames=updated_html_frames,@@ -717,9 +693,14 @@ class DomService:return dom_tree_node- enhanced_dom_tree_node = await _construct_enhanced_node(dom_tree['root'], initial_html_frames, initial_total_frame_offset)+ # Build enhanced DOM tree recursively+ start_tree_construction = time.time()+ enhanced_dom_tree_node = await _construct_enhanced_node(+ dom_tree['root'], initial_html_frames, initial_total_frame_offset, all_frames+ )+ timing_info['construct_enhanced_tree_ms'] = (time.time() - start_tree_construction) * 1000- return enhanced_dom_tree_node+ return enhanced_dom_tree_node, timing_info@observe_debug(ignore_input=True, ignore_output=True, name='get_serialized_dom_tree')async def get_serialized_dom_tree(@@ -730,23 +711,40 @@ class DomService:Returns:Tuple of (serialized_dom_state, enhanced_dom_tree_root, timing_info)"""+ timing_info: dict[str, float] = {}# Use current target (None means use current)assert self.browser_session.current_target_id is not None- enhanced_dom_tree = await self.get_dom_tree(target_id=self.browser_session.current_target_id)- start = time.time()+ # Fetch all_frames once at the start to avoid redundant CDP calls during recursive DOM construction+ start_frames = time.time()+ all_frames, _ = await self.browser_session.get_all_frames()+ timing_info['get_all_frames_ms'] = (time.time() - start_frames) * 1000++ # Build DOM tree (includes CDP calls for snapshot, DOM, AX tree)+ start_dom_tree = time.time()+ enhanced_dom_tree, dom_tree_timing = await self.get_dom_tree(+ target_id=self.browser_session.current_target_id,+ all_frames=all_frames,+ )+ timing_info['get_dom_tree_total_ms'] = (time.time() - start_dom_tree) * 1000++ # Add sub-timings from DOM tree construction+ for key, value in dom_tree_timing.items():+ timing_info[key] = value++ # Serialize DOM tree for LLM+ start_serialize = time.time()serialized_dom_state, serializer_timing = DOMTreeSerializer(enhanced_dom_tree, previous_cached_state, paint_order_filtering=self.paint_order_filtering).serialize_accessible_elements()+ timing_info['serialize_dom_tree_ms'] = (time.time() - start_serialize) * 1000- end = time.time()- serialize_total_timing = {'serialize_dom_tree_total': end - start}-- # Combine all timing info- all_timing = {**serializer_timing, **serialize_total_timing}+ # Add serializer sub-timings (convert to ms)+ for key, value in serializer_timing.items():+ timing_info[f'{key}_ms'] = value * 1000- return serialized_dom_state, enhanced_dom_tree, all_timing+ return serialized_dom_state, enhanced_dom_tree, timing_info@staticmethoddef detect_pagination_buttons(selector_map: dict[int, EnhancedDOMTreeNode]) -> list[dict[str, str | int | bool]]: