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
include ax_name for rerun history element hash
Saurav Pandae9c047be9226
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 05be0364d..bbada4157 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -2913,21 +2913,30 @@ class Agent(Generic[Context, AgentStructuredOutput]):"""Update action indices based on current page state.Returns updated action or None if element cannot be found.++ Matching strategy:+ 1. Try element_hash match (includes ax_name for uniqueness)+ 2. Fall back to XPath matching (for backwards compatibility with old history files)"""if not historical_element or not browser_state_summary.dom_state.selector_map:return action- # selector_hash_map = {hash(e): e for e in browser_state_summary.dom_state.selector_map.values()}+ selector_map = browser_state_summary.dom_state.selector_map+ # Strategy 1: Hash match (hash now includes ax_name for uniqueness)highlight_index, current_element = next(- (- (highlight_index, element)- for highlight_index, element in browser_state_summary.dom_state.selector_map.items()- if element.element_hash == historical_element.element_hash- ),+ ((idx, elem) for idx, elem in selector_map.items() if elem.element_hash == historical_element.element_hash),(None, None),)+ # Strategy 2: XPath fallback (for backwards compatibility with old history files)+ if current_element is None and historical_element.x_path:+ for idx, elem in selector_map.items():+ if elem.xpath == historical_element.x_path:+ highlight_index, current_element = idx, elem+ self.logger.debug(f'Found element via XPath fallback: {historical_element.x_path}')+ break+if not current_element or highlight_index is None:return Nonediff --git a/browser_use/dom/views.py b/browser_use/dom/views.pyindex 868fa55dd..132e48b28 100644--- a/browser_use/dom/views.py+++ b/browser_use/dom/views.py@@ -758,7 +758,7 @@ class EnhancedDOMTreeNode:def __hash__(self) -> int:"""- Hash the element based on its parent branch path and attributes.+ Hash the element based on its parent branch path, attributes, and accessibility name.TODO: migrate this to use only backendNodeId + current SessionId"""@@ -771,8 +771,14 @@ class EnhancedDOMTreeNode:f'{k}={v}' for k, v in sorted((k, v) for k, v in self.attributes.items() if k in STATIC_ATTRIBUTES))- # Combine both for final hash- combined_string = f'{parent_branch_path_string}|{attributes_string}'+ # Include accessibility name (ax_name) if available - this helps distinguish+ # elements that have identical structure and attributes but different visible text+ ax_name = ''+ if self.ax_node and self.ax_node.name:+ ax_name = f'|ax_name={self.ax_node.name}'++ # Combine all for final hash+ combined_string = f'{parent_branch_path_string}|{attributes_string}{ax_name}'element_hash = hashlib.sha256(combined_string.encode()).hexdigest()# Convert to int for __hash__ return type - use first 16 chars and convert from hex to int@@ -875,6 +881,9 @@ class DOMInteractedElement:element_hash: int+ # Accessibility name (visible text) - used for fallback matching when hash/xpath fail+ ax_name: str | None = None+def to_dict(self) -> dict[str, Any]:return {'node_id': self.node_id,@@ -887,10 +896,16 @@ class DOMInteractedElement:'x_path': self.x_path,'element_hash': self.element_hash,'bounds': self.bounds.to_dict() if self.bounds else None,+ 'ax_name': self.ax_name,}@classmethoddef load_from_enhanced_dom_tree(cls, enhanced_dom_tree: EnhancedDOMTreeNode) -> 'DOMInteractedElement':+ # Extract accessibility name if available+ ax_name = None+ if enhanced_dom_tree.ax_node and enhanced_dom_tree.ax_node.name:+ ax_name = enhanced_dom_tree.ax_node.name+return cls(node_id=enhanced_dom_tree.node_id,backend_node_id=enhanced_dom_tree.backend_node_id,@@ -902,4 +917,5 @@ class DOMInteractedElement:bounds=enhanced_dom_tree.snapshot_node.bounds if enhanced_dom_tree.snapshot_node else None,x_path=enhanced_dom_tree.xpath,element_hash=hash(enhanced_dom_tree),+ ax_name=ax_name,)