Skip to content

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

ignore highlight elements

Matic Zavadlal76614f09d047
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.js
index 1ee3d8922..bcfcfe7ec 100644
--- a/browser_use/dom/buildDomTree.js
+++ b/browser_use/dom/buildDomTree.js
@@ -20,15 +20,17 @@
// Quick check to confirm the script receives focusHighlightIndex
console.log("focusHighlightIndex:", focusHighlightIndex);
+ const HIGHLIGHT_CONTAINER_ID = "playwright-highlight-container";
+
/**
* Highlights an element in the DOM and returns the index of the next element.
*/
function highlightElement(element, index, parentIframe = null) {
// Create or get highlight container
- let container = document.getElementById("playwright-highlight-container");
+ let container = document.getElementById(HIGHLIGHT_CONTAINER_ID);
if (!container) {
container = document.createElement("div");
- container.id = "playwright-highlight-container";
+ container.id = HIGHLIGHT_CONTAINER_ID;
container.style.position = "absolute";
container.style.pointerEvents = "none";
container.style.top = "0";
@@ -36,6 +38,7 @@
container.style.width = "100%";
container.style.height = "100%";
container.style.zIndex = "2147483647"; // Maximum z-index value
+
document.body.appendChild(container);
}
@@ -507,6 +510,12 @@
return null;
}
+ // NOTE: We skip highlight container nodes from the DOM tree
+ // by ignoring the container element itself and all its children.
+ if (node.id === HIGHLIGHT_CONTAINER_ID) {
+ return null;
+ }
+
// Special case for text nodes
if (node.nodeType === Node.TEXT_NODE) {
const textContent = node.textContent.trim();