Skip to content

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

fix discovery of elements that have both shadow and non shadow child nodes

Nick Sweeting73369f910c38
diff --git a/browser_use/browser/views.py b/browser_use/browser/views.py
index 3434d86e2..1c46d0c5c 100644
--- a/browser_use/browser/views.py
+++ b/browser_use/browser/views.py
@@ -14,6 +14,7 @@ class TabInfo(BaseModel):
page_id: int
url: str
title: str
+ parent_page_id: Optional[int] = None # parent page that contains this popup or cross-origin iframe
@dataclass
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.js
index 9286c097d..e86a5bd87 100644
--- a/browser_use/dom/buildDomTree.js
+++ b/browser_use/dom/buildDomTree.js
@@ -955,16 +955,16 @@
if (domElement) nodeData.children.push(domElement);
}
}
- // Handle shadow DOM
- else if (node.shadowRoot) {
- nodeData.shadowRoot = true;
- for (const child of node.shadowRoot.childNodes) {
- const domElement = buildDomTree(child, parentIframe);
- if (domElement) nodeData.children.push(domElement);
- }
- }
// Handle regular elements
else {
+ // Handle shadow DOM
+ if (node.shadowRoot) {
+ nodeData.shadowRoot = true;
+ for (const child of node.shadowRoot.childNodes) {
+ const domElement = buildDomTree(child, parentIframe);
+ if (domElement) nodeData.children.push(domElement);
+ }
+ }
for (const child of node.childNodes) {
const domElement = buildDomTree(child, parentIframe);
if (domElement) nodeData.children.push(domElement);