Skip to content

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

Fix: Ensure first element is correctly indexed in sibling list

shivam1a6516677ad5
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.js
index 22481e9fb..b158bce1e 100644
--- a/browser_use/dom/buildDomTree.js
+++ b/browser_use/dom/buildDomTree.js
@@ -383,6 +383,24 @@
}
}
+ function getElementPosition(currentElement) {
+ if (!currentElement.parentElement) {
+ return 0; // No parent means no siblings
+ }
+
+ const tagName = currentElement.nodeName.toLowerCase();
+
+ const siblings = Array.from(currentElement.parentElement.children)
+ .filter((sib) => sib.nodeName.toLowerCase() === tagName);
+
+ if (siblings.length === 1) {
+ return 0; // Only element of its type
+ }
+
+ const index = siblings.indexOf(currentElement) + 1; // 1-based index
+ return index;
+ }
+
/**
* Returns an XPath tree string for an element.
*/
@@ -400,20 +418,9 @@
break;
}
- let index = 0;
- let sibling = currentElement.previousSibling;
- while (sibling) {
- if (
- sibling.nodeType === Node.ELEMENT_NODE &&
- sibling.nodeName === currentElement.nodeName
- ) {
- index++;
- }
- sibling = sibling.previousSibling;
- }
-
+ const position = getElementPosition(currentElement);
const tagName = currentElement.nodeName.toLowerCase();
- const xpathIndex = index > 0 ? `[${index + 1}]` : "";
+ const xpathIndex = position > 0 ? `[${position}]` : "";
segments.unshift(`${tagName}${xpathIndex}`);
currentElement = currentElement.parentNode;