Skip to content

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

fixed inline flex elements (multiline text) clickability, highlights

Gregor Žunič753651f7939e
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.js
index d69b4601c..f26dcc73a 100644
--- a/browser_use/dom/buildDomTree.js
+++ b/browser_use/dom/buildDomTree.js
@@ -193,6 +193,12 @@
function highlightElement(element, index, parentIframe = null) {
if (!element) return index;
+ // Store overlays and the single label for updating
+ const overlays = [];
+ let label = null;
+ let labelWidth = 20; // Approximate label width
+ let labelHeight = 16; // Approximate label height
+
try {
// Create or get highlight container
let container = document.getElementById(HIGHLIGHT_CONTAINER_ID);
@@ -209,13 +215,10 @@
document.body.appendChild(container);
}
- // Get element position
- const rect = measureDomOperation(
- () => element.getBoundingClientRect(),
- 'getBoundingClientRect'
- );
+ // Get element client rects
+ const rects = element.getClientRects(); // Use getClientRects()
- if (!rect) return index;
+ if (!rects || rects.length === 0) return index; // Exit if no rects
// Generate a color based on the index
const colors = [
@@ -236,99 +239,147 @@
const baseColor = colors[colorIndex];
const backgroundColor = baseColor + "1A"; // 10% opacity version of the color
- // Create highlight overlay
- const overlay = document.createElement("div");
- overlay.style.position = "fixed";
- overlay.style.border = `2px solid ${baseColor}`;
- overlay.style.backgroundColor = backgroundColor;
- overlay.style.pointerEvents = "none";
- overlay.style.boxSizing = "border-box";
-
- // Get element position
+ // Get iframe offset if necessary
let iframeOffset = { x: 0, y: 0 };
-
- // If element is in an iframe, calculate iframe offset
if (parentIframe) {
- const iframeRect = parentIframe.getBoundingClientRect();
+ const iframeRect = parentIframe.getBoundingClientRect(); // Keep getBoundingClientRect for iframe offset
iframeOffset.x = iframeRect.left;
iframeOffset.y = iframeRect.top;
}
- // Calculate position
- const top = rect.top + iframeOffset.y;
- const left = rect.left + iframeOffset.x;
+ // Create highlight overlays for each client rect
+ for (const rect of rects) {
+ if (rect.width === 0 || rect.height === 0) continue; // Skip empty rects
- overlay.style.top = `${top}px`;
- overlay.style.left = `${left}px`;
- overlay.style.width = `${rect.width}px`;
- overlay.style.height = `${rect.height}px`;
+ const overlay = document.createElement("div");
+ overlay.style.position = "fixed";
+ overlay.style.border = `2px solid ${baseColor}`;
+ overlay.style.backgroundColor = backgroundColor;
+ overlay.style.pointerEvents = "none";
+ overlay.style.boxSizing = "border-box";
+
+ const top = rect.top + iframeOffset.y;
+ const left = rect.left + iframeOffset.x;
+
+ overlay.style.top = `${top}px`;
+ overlay.style.left = `${left}px`;
+ overlay.style.width = `${rect.width}px`;
+ overlay.style.height = `${rect.height}px`;
+
+ container.appendChild(overlay);
+ overlays.push({ element: overlay, initialRect: rect }); // Store overlay and its rect
+ }
- // Create and position label
- const label = document.createElement("div");
+ // Create and position a single label relative to the first rect
+ const firstRect = rects[0];
+ label = document.createElement("div");
label.className = "playwright-highlight-label";
label.style.position = "fixed";
label.style.background = baseColor;
label.style.color = "white";
label.style.padding = "1px 4px";
label.style.borderRadius = "4px";
- label.style.fontSize = `${Math.min(12, Math.max(8, rect.height / 2))}px`;
+ label.style.fontSize = `${Math.min(12, Math.max(8, firstRect.height / 2))}px`;
label.textContent = index;
- const labelWidth = 20;
- const labelHeight = 16;
+ labelWidth = label.offsetWidth > 0 ? label.offsetWidth : labelWidth; // Update actual width if possible
+ labelHeight = label.offsetHeight > 0 ? label.offsetHeight : labelHeight; // Update actual height if possible
- let labelTop = top + 2;
- let labelLeft = left + rect.width - labelWidth - 2;
+ const firstRectTop = firstRect.top + iframeOffset.y;
+ const firstRectLeft = firstRect.left + iframeOffset.x;
- if (rect.width < labelWidth + 4 || rect.height < labelHeight + 4) {
- labelTop = top - labelHeight - 2;
- labelLeft = left + rect.width - labelWidth;
+ let labelTop = firstRectTop + 2;
+ let labelLeft = firstRectLeft + firstRect.width - labelWidth - 2;
+
+ // Adjust label position if first rect is too small
+ if (firstRect.width < labelWidth + 4 || firstRect.height < labelHeight + 4) {
+ labelTop = firstRectTop - labelHeight - 2;
+ labelLeft = firstRectLeft + firstRect.width - labelWidth; // Align with right edge
+ if (labelLeft < iframeOffset.x) labelLeft = firstRectLeft; // Prevent going off-left
}
+ // Ensure label stays within viewport bounds slightly better
+ labelTop = Math.max(0, Math.min(labelTop, window.innerHeight - labelHeight));
+ labelLeft = Math.max(0, Math.min(labelLeft, window.innerWidth - labelWidth));
+
+
label.style.top = `${labelTop}px`;
label.style.left = `${labelLeft}px`;
- // Add to container
- container.appendChild(overlay);
container.appendChild(label);
- // Update positions on scroll
+ // Update positions on scroll/resize
const updatePositions = () => {
- const newRect = element.getBoundingClientRect();
+ const newRects = element.getClientRects(); // Get fresh rects
let newIframeOffset = { x: 0, y: 0 };
if (parentIframe) {
- const iframeRect = parentIframe.getBoundingClientRect();
+ const iframeRect = parentIframe.getBoundingClientRect(); // Keep getBoundingClientRect for iframe
newIframeOffset.x = iframeRect.left;
newIframeOffset.y = iframeRect.top;
}
- const newTop = newRect.top + newIframeOffset.y;
- const newLeft = newRect.left + newIframeOffset.x;
+ // Update each overlay
+ overlays.forEach((overlayData, i) => {
+ if (i < newRects.length) { // Check if rect still exists
+ const newRect = newRects[i];
+ const newTop = newRect.top + newIframeOffset.y;
+ const newLeft = newRect.left + newIframeOffset.x;
+
+ overlayData.element.style.top = `${newTop}px`;
+ overlayData.element.style.left = `${newLeft}px`;
+ overlayData.element.style.width = `${newRect.width}px`;
+ overlayData.element.style.height = `${newRect.height}px`;
+ overlayData.element.style.display = (newRect.width === 0 || newRect.height === 0) ? 'none' : 'block';
+ } else {
+ // If fewer rects now, hide extra overlays
+ overlayData.element.style.display = 'none';
+ }
+ });
+
+ // If there are fewer new rects than overlays, hide the extras
+ if (newRects.length < overlays.length) {
+ for (let i = newRects.length; i < overlays.length; i++) {
+ overlays[i].element.style.display = 'none';
+ }
+ }
+
+ // Update label position based on the first new rect
+ if (label && newRects.length > 0) {
+ const firstNewRect = newRects[0];
+ const firstNewRectTop = firstNewRect.top + newIframeOffset.y;
+ const firstNewRectLeft = firstNewRect.left + newIframeOffset.x;
- overlay.style.top = `${newTop}px`;
- overlay.style.left = `${newLeft}px`;
- overlay.style.width = `${newRect.width}px`;
- overlay.style.height = `${newRect.height}px`;
+ let newLabelTop = firstNewRectTop + 2;
+ let newLabelLeft = firstNewRectLeft + firstNewRect.width - labelWidth - 2;
- let newLabelTop = newTop + 2;
- let newLabelLeft = newLeft + newRect.width - labelWidth - 2;
+ if (firstNewRect.width < labelWidth + 4 || firstNewRect.height < labelHeight + 4) {
+ newLabelTop = firstNewRectTop - labelHeight - 2;
+ newLabelLeft = firstNewRectLeft + firstNewRect.width - labelWidth;
+ if (newLabelLeft < newIframeOffset.x) newLabelLeft = firstNewRectLeft;
+ }
- if (newRect.width < labelWidth + 4 || newRect.height < labelHeight + 4) {
- newLabelTop = newTop - labelHeight - 2;
- newLabelLeft = newLeft + newRect.width - labelWidth;
- }
+ // Ensure label stays within viewport bounds
+ newLabelTop = Math.max(0, Math.min(newLabelTop, window.innerHeight - labelHeight));
+ newLabelLeft = Math.max(0, Math.min(newLabelLeft, window.innerWidth - labelWidth));
- label.style.top = `${newLabelTop}px`;
- label.style.left = `${newLabelLeft}px`;
+ label.style.top = `${newLabelTop}px`;
+ label.style.left = `${newLabelLeft}px`;
+ label.style.display = 'block';
+ } else if (label) {
+ // Hide label if element has no rects anymore
+ label.style.display = 'none';
+ }
};
- window.addEventListener('scroll', updatePositions);
+ window.addEventListener('scroll', updatePositions, true); // Use capture phase
window.addEventListener('resize', updatePositions);
+ // TODO: Add cleanup logic to remove listeners and elements when done.
+
return index + 1;
} finally {
- popTiming('highlighting');
+ // popTiming('highlighting'); // Assuming this was a typo and should be removed or corrected
}
}
@@ -378,20 +429,36 @@
try {
const range = document.createRange();
range.selectNodeContents(textNode);
- const rect = range.getBoundingClientRect();
+ const rects = range.getClientRects(); // Use getClientRects for Range
- // Simple size check
- if (rect.width === 0 || rect.height === 0) {
+ if (!rects || rects.length === 0) {
return false;
}
- // Simple viewport check without scroll calculations
- const isInViewport = !(
- rect.bottom < -viewportExpansion ||
- rect.top > window.innerHeight + viewportExpansion ||
- rect.right < -viewportExpansion ||
- rect.left > window.innerWidth + viewportExpansion
- ) || viewportExpansion === -1;
+ let isAnyRectVisible = false;
+ let isAnyRectInViewport = false;
+
+ for (const rect of rects) {
+ // Check size
+ if (rect.width > 0 && rect.height > 0) {
+ isAnyRectVisible = true;
+
+ // Viewport check for this rect
+ if (!(
+ rect.bottom < -viewportExpansion ||
+ rect.top > window.innerHeight + viewportExpansion ||
+ rect.right < -viewportExpansion ||
+ rect.left > window.innerWidth + viewportExpansion
+ ) || viewportExpansion === -1) {
+ isAnyRectInViewport = true;
+ break; // Found a visible rect in viewport, no need to check others
+ }
+ }
+ }
+
+ if (!isAnyRectVisible || !isAnyRectInViewport) {
+ return false;
+ }
// Check parent visibility
const parentElement = textNode.parentElement;
@@ -721,25 +788,31 @@
* Checks if an element is the topmost element at its position.
*/
function isTopElement(element) {
- const rect = getCachedBoundingRect(element);
+ const rects = element.getClientRects(); // Use getClientRects
- if (viewportExpansion <= 0) {
- if (rect.bottom < 0 ||
- rect.top > window.innerHeight ||
- rect.right < 0 ||
- rect.left > window.innerWidth) {
- return false;
- }
- } else {
- // For positive viewportExpansion, only expand in Y direction
- if (rect.bottom < -viewportExpansion ||
+ if (!rects || rects.length === 0) {
+ return false; // No geometry, cannot be top
+ }
+
+ let isAnyRectInViewport = false;
+ for (const rect of rects) {
+ // Use the same logic as isInExpandedViewport check
+ if (rect.width > 0 && rect.height > 0 && !( // Only check non-empty rects
+ rect.bottom < -viewportExpansion ||
rect.top > window.innerHeight + viewportExpansion ||
- rect.right < 0 ||
- rect.left > window.innerWidth) {
- return false;
+ rect.right < -viewportExpansion ||
+ rect.left > window.innerWidth + viewportExpansion
+ ) || viewportExpansion === -1) {
+ isAnyRectInViewport = true;
+ break;
}
}
+ if (!isAnyRectInViewport) {
+ return false; // All rects are outside the viewport area
+ }
+
+
// Find the correct document context and root element
let doc = element.ownerDocument;
@@ -751,8 +824,8 @@
// For shadow DOM, we need to check within its own root context
const shadowRoot = element.getRootNode();
if (shadowRoot instanceof ShadowRoot) {
- const centerX = rect.left + rect.width / 2;
- const centerY = rect.top + rect.height / 2;
+ const centerX = rects[Math.floor(rects.length / 2)].left + rects[Math.floor(rects.length / 2)].width / 2;
+ const centerY = rects[Math.floor(rects.length / 2)].top + rects[Math.floor(rects.length / 2)].height / 2;
try {
const topEl = measureDomOperation(
@@ -773,8 +846,8 @@
}
// For elements in viewport, check if they're topmost
- const centerX = rect.left + rect.width / 2;
- const centerY = rect.top + rect.height / 2;
+ const centerX = rects[Math.floor(rects.length / 2)].left + rects[Math.floor(rects.length / 2)].width / 2;
+ const centerY = rects[Math.floor(rects.length / 2)].top + rects[Math.floor(rects.length / 2)].height / 2;
try {
const topEl = document.elementFromPoint(centerX, centerY);
@@ -799,15 +872,39 @@
return true;
}
- const rect = getCachedBoundingRect(element);
+ const rects = element.getClientRects(); // Use getClientRects
- // Simple viewport check without scroll calculations
- return !(
- rect.bottom < -viewportExpansion ||
- rect.top > window.innerHeight + viewportExpansion ||
- rect.right < -viewportExpansion ||
- rect.left > window.innerWidth + viewportExpansion
- );
+ if (!rects || rects.length === 0) {
+ // Fallback to getBoundingClientRect if getClientRects is empty,
+ // useful for elements like <svg> that might not have client rects but have a bounding box.
+ const boundingRect = getCachedBoundingRect(element);
+ if (!boundingRect || boundingRect.width === 0 || boundingRect.height === 0) {
+ return false;
+ }
+ return !(
+ boundingRect.bottom < -viewportExpansion ||
+ boundingRect.top > window.innerHeight + viewportExpansion ||
+ boundingRect.right < -viewportExpansion ||
+ boundingRect.left > window.innerWidth + viewportExpansion
+ );
+ }
+
+
+ // Check if *any* client rect is within the viewport
+ for (const rect of rects) {
+ if (rect.width === 0 || rect.height === 0) continue; // Skip empty rects
+
+ if (!(
+ rect.bottom < -viewportExpansion ||
+ rect.top > window.innerHeight + viewportExpansion ||
+ rect.right < -viewportExpansion ||
+ rect.left > window.innerWidth + viewportExpansion
+ )) {
+ return true; // Found at least one rect in the viewport
+ }
+ }
+
+ return false; // No rects were found in the viewport
}
// Add this new helper function
@@ -936,21 +1033,24 @@
// Early viewport check - only filter out elements clearly outside viewport
if (viewportExpansion !== -1) {
- const rect = getCachedBoundingRect(node);
+ const rect = getCachedBoundingRect(node); // Keep for initial quick check
const style = getCachedComputedStyle(node);
// Skip viewport check for fixed/sticky elements as they may appear anywhere
const isFixedOrSticky = style && (style.position === 'fixed' || style.position === 'sticky');
- // Check if element has actual dimensions
+ // Check if element has actual dimensions using offsetWidth/Height (quick check)
const hasSize = node.offsetWidth > 0 || node.offsetHeight > 0;
+ // Use getBoundingClientRect for the quick OUTSIDE check.
+ // isInExpandedViewport will do the more accurate check later if needed.
if (!rect || (!isFixedOrSticky && !hasSize && (
rect.bottom < -viewportExpansion ||
rect.top > window.innerHeight + viewportExpansion ||
rect.right < -viewportExpansion ||
rect.left > window.innerWidth + viewportExpansion
))) {
+ // console.log("Skipping node outside viewport (quick check):", node.tagName, rect);
if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
return null;
}
@@ -976,23 +1076,30 @@
// Check interactivity
if (node.nodeType === Node.ELEMENT_NODE) {
- nodeData.isVisible = isElementVisible(node);
+ nodeData.isVisible = isElementVisible(node); // isElementVisible uses offsetWidth/Height, which is fine
if (nodeData.isVisible) {
+ // isTopElement now uses getClientRects
nodeData.isTopElement = isTopElement(node);
if (nodeData.isTopElement) {
nodeData.isInteractive = isInteractiveElement(node);
if (nodeData.isInteractive) {
- nodeData.isInViewport = true;
- nodeData.highlightIndex = highlightIndex++;
-
- if (doHighlightElements) {
- if (focusHighlightIndex >= 0) {
- if (focusHighlightIndex === nodeData.highlightIndex) {
+ // isInExpandedViewport now uses getClientRects
+ nodeData.isInViewport = isInExpandedViewport(node, viewportExpansion);
+ if (nodeData.isInViewport) { // Only highlight if in viewport based on getClientRects
+ nodeData.highlightIndex = highlightIndex++;
+
+ if (doHighlightElements) {
+ if (focusHighlightIndex >= 0) {
+ if (focusHighlightIndex === nodeData.highlightIndex) {
+ highlightElement(node, nodeData.highlightIndex, parentIframe);
+ }
+ } else {
highlightElement(node, nodeData.highlightIndex, parentIframe);
}
- } else {
- highlightElement(node, nodeData.highlightIndex, parentIframe);
}
+ } else {
+ // Element is interactive and top, but determined to be outside viewport by getClientRects
+ // console.log("Interactive element outside viewport:", node.tagName, nodeData.xpath);
}
}
}