Skip to content

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

fix: Locate element by xpath when rerunning

Sean Gu7ccf09fb76a0
diff --git a/browser_use/dom/history_tree_processor/service.py b/browser_use/dom/history_tree_processor/service.py
index 32dd3e18b..eac14d7eb 100644
--- a/browser_use/dom/history_tree_processor/service.py
+++ b/browser_use/dom/history_tree_processor/service.py
@@ -64,17 +64,19 @@ class HistoryTreeProcessor:
dom_history_element.entire_parent_branch_path
)
attributes_hash = HistoryTreeProcessor._attributes_hash(dom_history_element.attributes)
+ xpath_hash = HistoryTreeProcessor._xpath_hash(dom_history_element.xpath)
- return HashedDomElement(branch_path_hash, attributes_hash)
+ return HashedDomElement(branch_path_hash, attributes_hash, xpath_hash)
@staticmethod
def _hash_dom_element(dom_element: DOMElementNode) -> HashedDomElement:
parent_branch_path = HistoryTreeProcessor._get_parent_branch_path(dom_element)
branch_path_hash = HistoryTreeProcessor._parent_branch_path_hash(parent_branch_path)
attributes_hash = HistoryTreeProcessor._attributes_hash(dom_element.attributes)
+ xpath_hash = HistoryTreeProcessor._xpath_hash(dom_element.xpath)
# text_hash = DomTreeProcessor._text_hash(dom_element)
- return HashedDomElement(branch_path_hash, attributes_hash)
+ return HashedDomElement(branch_path_hash, attributes_hash, xpath_hash)
@staticmethod
def _get_parent_branch_path(dom_element: DOMElementNode) -> list[str]:
@@ -98,6 +100,10 @@ class HistoryTreeProcessor:
attributes_string = ''.join(f'{key}={value}' for key, value in attributes.items())
return hashlib.sha256(attributes_string.encode()).hexdigest()
+ @staticmethod
+ def _xpath_hash(xpath: str) -> str:
+ return hashlib.sha256(xpath.encode()).hexdigest()
+
@staticmethod
def _text_hash(dom_element: DOMElementNode) -> str:
""" """
diff --git a/browser_use/dom/history_tree_processor/view.py b/browser_use/dom/history_tree_processor/view.py
index a6604cf10..f044b78d8 100644
--- a/browser_use/dom/history_tree_processor/view.py
+++ b/browser_use/dom/history_tree_processor/view.py
@@ -10,6 +10,7 @@ class HashedDomElement:
branch_path_hash: str
attributes_hash: str
+ xpath_hash: str
# text_hash: str