Skip to content

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

Check scrolling for text nodes

Magnus Müller9824b678c8ec
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.js
index 8ed92e6b0..5ee770081 100644
--- a/browser_use/dom/buildDomTree.js
+++ b/browser_use/dom/buildDomTree.js
@@ -564,12 +564,34 @@
const range = document.createRange();
range.selectNodeContents(textNode);
const rect = range.getBoundingClientRect();
+
+ // Get effective scroll for the text node's parent element
+ const { scrollX, scrollY } = getEffectiveScroll(textNode.parentElement);
+
+ // Calculate expanded viewport boundaries including effective scroll
+ const viewportTop = -viewportExpansion + scrollY;
+ const viewportLeft = -viewportExpansion + scrollX;
+ const viewportBottom = window.innerHeight + viewportExpansion + scrollY;
+ const viewportRight = window.innerWidth + viewportExpansion + scrollX;
+
+ // Get absolute text node position using effective scroll
+ const absTop = rect.top + scrollY;
+ const absLeft = rect.left + scrollX;
+ const absBottom = rect.bottom + scrollY;
+ const absRight = rect.right + scrollX;
+
+ // Check if text node is within expanded viewport
+ const isInExpandedViewport = !(
+ absBottom < viewportTop ||
+ absTop > viewportBottom ||
+ absRight < viewportLeft ||
+ absLeft > viewportRight
+ );
return (
rect.width !== 0 &&
rect.height !== 0 &&
- rect.top >= 0 &&
- rect.top <= window.innerHeight &&
+ isInExpandedViewport &&
textNode.parentElement?.checkVisibility({
checkOpacity: true,
checkVisibilityCSS: true,