Skip to content

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

Revert extraction layer to previous state to include text again correctly.

Magnus Müller2446f6aaa7bd
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.js
index 32182b770..a093ca47e 100644
--- a/browser_use/dom/buildDomTree.js
+++ b/browser_use/dom/buildDomTree.js
@@ -462,6 +462,34 @@
return false;
}
+ // Special handling for cookie banner elements
+ const isCookieBannerElement =
+ (typeof element.closest === 'function') && (
+ element.closest('[id*="onetrust"]') ||
+ element.closest('[class*="onetrust"]') ||
+ element.closest('[data-nosnippet="true"]') ||
+ element.closest('[aria-label*="cookie"]')
+ );
+
+ if (isCookieBannerElement) {
+ // Check if it's a button or interactive element within the banner
+ if (
+ element.tagName.toLowerCase() === 'button' ||
+ element.getAttribute('role') === 'button' ||
+ element.onclick ||
+ element.getAttribute('onclick') ||
+ (element.classList && (
+ element.classList.contains('ot-sdk-button') ||
+ element.classList.contains('accept-button') ||
+ element.classList.contains('reject-button')
+ )) ||
+ element.getAttribute('aria-label')?.toLowerCase().includes('accept') ||
+ element.getAttribute('aria-label')?.toLowerCase().includes('reject')
+ ) {
+ return true;
+ }
+ }
+
// Base interactive elements and roles
const interactiveElements = new Set([
"a", "button", "details", "embed", "input", "menu", "menuitem",
@@ -506,8 +534,43 @@
if (hasInteractiveRole) return true;
+ // Additional checks for cookie banners and consent UI
+ const isCookieBanner =
+ element.id?.toLowerCase().includes('cookie') ||
+ element.id?.toLowerCase().includes('consent') ||
+ element.id?.toLowerCase().includes('notice') ||
+ (element.classList && (
+ element.classList.contains('otCenterRounded') ||
+ element.classList.contains('ot-sdk-container')
+ )) ||
+ element.getAttribute('data-nosnippet') === 'true' ||
+ element.getAttribute('aria-label')?.toLowerCase().includes('cookie') ||
+ element.getAttribute('aria-label')?.toLowerCase().includes('consent') ||
+ (element.tagName.toLowerCase() === 'div' && (
+ element.id?.includes('onetrust') ||
+ (element.classList && (
+ element.classList.contains('onetrust') ||
+ element.classList.contains('cookie') ||
+ element.classList.contains('consent')
+ ))
+ ));
+
+ if (isCookieBanner) return true;
+
+ // Additional check for buttons in cookie banners
+ const isInCookieBanner = typeof element.closest === 'function' && element.closest(
+ '[id*="cookie"],[id*="consent"],[class*="cookie"],[class*="consent"],[id*="onetrust"]'
+ );
-
+ if (isInCookieBanner && (
+ element.tagName.toLowerCase() === 'button' ||
+ element.getAttribute('role') === 'button' ||
+ (element.classList && element.classList.contains('button')) ||
+ element.onclick ||
+ element.getAttribute('onclick')
+ )) {
+ return true;
+ }
// Get computed style
const style = window.getComputedStyle(element);
@@ -789,7 +852,7 @@
return id;
}
- // Quick checks for element nodes - skip filtering for OneTrust elements
+ // Quick checks for element nodes
if (node.nodeType === Node.ELEMENT_NODE && !isElementAccepted(node)) {
if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
return null;
@@ -798,16 +861,12 @@
// Check viewport if needed
if (viewportExpansion !== -1) {
const rect = getCachedBoundingRect(node);
- const style = getCachedComputedStyle(node);
-
- // Skip viewport check for OneTrust elements, fixed/sticky position elements, or if element has no rect
- const isOneTrust = node.id?.includes('onetrust-') || node.closest('#onetrust-consent-sdk');
- if (!rect || (!isOneTrust && !style?.position?.match(/fixed|sticky/) && (
+ if (!rect || (
rect.bottom < -viewportExpansion ||
rect.top > window.innerHeight + viewportExpansion ||
rect.right < -viewportExpansion ||
rect.left > window.innerWidth + viewportExpansion
- ))) {
+ )) {
if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
return null;
}
@@ -821,12 +880,16 @@
children: [],
};
- // Get attributes
- const attributeNames = node.getAttributeNames?.() || [];
- for (const name of attributeNames) {
- nodeData.attributes[name] = node.getAttribute(name);
+ // Get attributes for interactive elements or potential text containers
+ if (isInteractiveCandidate(node) || node.tagName.toLowerCase() === 'iframe' || node.tagName.toLowerCase() === 'body') {
+ const attributeNames = node.getAttributeNames?.() || [];
+ for (const name of attributeNames) {
+ nodeData.attributes[name] = node.getAttribute(name);
+ }
}
+ // if (isInteractiveCandidate(node)) {
+
// Check interactivity
if (node.nodeType === Node.ELEMENT_NODE) {
nodeData.isVisible = isElementVisible(node);
@@ -852,39 +915,16 @@
}
}
- // Process children, with special handling for iframes, shadow DOM, and custom elements
+ // Process children, with special handling for iframes and rich text editors
if (node.tagName) {
const tagName = node.tagName.toLowerCase();
- // Handle shadow DOM
- if (node.shadowRoot) {
- const shadowChildren = Array.from(node.shadowRoot.children);
- for (const child of shadowChildren) {
- const domElement = buildDomTree(child, parentIframe);
- if (domElement) nodeData.children.push(domElement);
- }
- }
-
- // Handle custom elements that might have shadow roots
- if (tagName.includes('-')) {
- // Try to access shadow root even if not directly exposed
- const shadowRoot = node.shadowRoot || node.openOrClosedShadowRoot;
- if (shadowRoot) {
- const shadowChildren = Array.from(shadowRoot.children);
- for (const child of shadowChildren) {
- const domElement = buildDomTree(child, parentIframe);
- if (domElement) nodeData.children.push(domElement);
- }
- }
- }
-
// Handle iframes
if (tagName === "iframe") {
try {
const iframeDoc = node.contentDocument || node.contentWindow?.document;
if (iframeDoc) {
- const iframeChildren = Array.from(iframeDoc.body.children);
- for (const child of iframeChildren) {
+ for (const child of iframeDoc.childNodes) {
const domElement = buildDomTree(child, node);
if (domElement) nodeData.children.push(domElement);
}
@@ -893,13 +933,41 @@
console.warn("Unable to access iframe:", e);
}
}
-
- // Process regular children
- const children = Array.from(node.children);
- for (const child of children) {
- const domElement = buildDomTree(child, parentIframe);
- if (domElement) nodeData.children.push(domElement);
+ // Handle rich text editors and contenteditable elements
+ else if (
+ node.isContentEditable ||
+ node.getAttribute("contenteditable") === "true" ||
+ node.id === "tinymce" ||
+ node.classList.contains("mce-content-body") ||
+ (tagName === "body" && node.getAttribute("data-id")?.startsWith("mce_"))
+ ) {
+ // Process all child nodes to capture formatted text
+ for (const child of node.childNodes) {
+ const domElement = buildDomTree(child, parentIframe);
+ 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 {
+ for (const child of node.childNodes) {
+ const domElement = buildDomTree(child, parentIframe);
+ if (domElement) nodeData.children.push(domElement);
+ }
+ }
+ }
+
+ // Skip empty anchor tags
+ if (nodeData.tagName === 'a' && nodeData.children.length === 0 && !nodeData.attributes.href) {
+ if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
+ return null;
}
const id = `${ID.current++}`;