Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Simplified buildDomTree.js
magmueller969e31140c6a
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.jsindex e12d552e0..16f6edb6f 100644--- a/browser_use/dom/buildDomTree.js+++ b/browser_use/dom/buildDomTree.js@@ -1,20 +1,10 @@-(doHighlightElements = true) => {- let highlightIndex = 0;- const COLORS = [- '#FF4444', // Red- '#44AA44', // Darker Green- '#4444FF', // Blue- '#FF44FF', // Magenta- '#44CCCC', // Darker Cyan- '#FF8844', // Orange- '#9944FF', // Purple- '#FF4488', // Pink- ];-- const usedPositions = new Set();-- // Move createHighlightContainer to the top level scope- function createHighlightContainer() {+(+ doHighlightElements = true+) => {+ let highlightIndex = 0; // Reset highlight index++ function highlightElement(element, index, parentIframe = null) {+ // Create or get highlight containerlet container = document.getElementById('playwright-highlight-container');if (!container) {container = document.createElement('div');@@ -25,206 +15,92 @@container.style.left = '0';container.style.width = '100%';container.style.height = '100%';- container.style.zIndex = '2147483647';+ container.style.zIndex = '2147483647'; // Maximum z-index valuedocument.documentElement.appendChild(container);}- return container;- }-- function getIframeOffset(iframe) {- let offset = { top: 0, left: 0 };- try {- if (!iframe) return offset;-- const rect = iframe.getBoundingClientRect();- offset.top = rect.top;- offset.left = rect.left;-- if (iframe.contentWindow) {- offset.top += iframe.contentWindow.scrollY || 0;- offset.left += iframe.contentWindow.scrollX || 0;- }-- offset.top += window.scrollY;- offset.left += window.scrollX;-- return offset;- } catch (e) {- console.warn('Error calculating iframe offset:', e);- return offset;- }- }- function getAbsolutePosition(element, parentIframe) {+ // Generate a color based on the index+ const colors = [+ '#FF0000', '#00FF00', '#0000FF', '#FFA500',+ '#800080', '#008080', '#FF69B4', '#4B0082',+ '#FF4500', '#2E8B57', '#DC143C', '#4682B4'+ ];+ const colorIndex = index % colors.length;+ 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 = 'absolute';+ overlay.style.border = `2px solid ${baseColor}`;+ overlay.style.backgroundColor = backgroundColor;+ overlay.style.pointerEvents = 'none';+ overlay.style.boxSizing = 'border-box';++ // Position overlay based on elementconst rect = element.getBoundingClientRect();- const iframeOffset = getIframeOffset(parentIframe);-- return {- top: rect.top + iframeOffset.top,- left: rect.left + iframeOffset.left,- width: rect.width,- height: rect.height- };- }-- function findOptimalLabelPosition(elementPos, viewport) {- const isSmallElement = elementPos.width < 30 || elementPos.height < 20;- const labelDims = {- width: isSmallElement ? 16 : 20,- height: isSmallElement ? 14 : 16- };- const margin = 4;-- // Helper to check if positions are horizontally adjacent- function isHorizontallyAdjacent(pos) {- return Array.from(usedPositions).some(usedPos => {- const [usedTop, usedLeft] = usedPos.split(',').map(Number);- const verticalOverlap = Math.abs(usedTop - pos.top) < labelDims.height;- const horizontalProximity = Math.abs(usedLeft - pos.left) < labelDims.width * 2;- return verticalOverlap && horizontalProximity;- });+ let top = rect.top;+ let left = rect.left;++ // Adjust position if element is inside an iframe+ if (parentIframe) {+ const iframeRect = parentIframe.getBoundingClientRect();+ top += iframeRect.top;+ left += iframeRect.left;}-- // Calculate positions relative to the element- const positions = [- // Left side (default)- {- top: elementPos.top + (elementPos.height - labelDims.height) / 2,- left: elementPos.left - labelDims.width - margin,- score: 100,- position: 'left'- },- // Inside (if large enough)- elementPos.width >= 40 && elementPos.height >= 25 ? {- top: elementPos.top + margin,- left: elementPos.left + margin,- score: 90,- position: 'inside'- } : null,- // Above- {- top: elementPos.top - labelDims.height - margin,- left: elementPos.left + (elementPos.width - labelDims.width) / 2,- score: 80,- position: 'above'- },- // Below- {- top: elementPos.top + elementPos.height + margin,- left: elementPos.left + (elementPos.width - labelDims.width) / 2,- score: 70,- position: 'below'- },- // Right- {- top: elementPos.top + (elementPos.height - labelDims.height) / 2,- left: elementPos.left + elementPos.width + margin,- score: 60,- position: 'right'- }- ].filter(Boolean);-- return positions- .map(pos => {- let score = pos.score;- const posKey = `${Math.round(pos.top)},${Math.round(pos.left)}`;-- // Check for existing labels- if (usedPositions.has(posKey)) {- score -= 100; // Make it very unlikely to overlap- }-- // Heavily penalize horizontally adjacent positions- if (isHorizontallyAdjacent(pos)) {- score -= 50;- }-- // Check viewport bounds- if (pos.top < 0 || pos.top + labelDims.height > viewport.height ||- pos.left < 0 || pos.left + labelDims.width > viewport.width) {- score -= 60;- }-- // Check for element overlaps- try {- const elementsAtPos = document.elementsFromPoint(pos.left, pos.top);- if (elementsAtPos.length > 2) {- score -= 40;- }- } catch (e) {- score -= 20;- }-- // Prefer above/below for horizontally adjacent elements- if (pos.position === 'above' || pos.position === 'below') {- const hasHorizontalNeighbors = Array.from(usedPositions).some(usedPos => {- const [usedTop, usedLeft] = usedPos.split(',').map(Number);- return Math.abs(usedLeft - elementPos.left) < elementPos.width * 2;- });- if (hasHorizontalNeighbors) {- score += 30;- }- }-- return { ...pos, score };- })- .sort((a, b) => b.score - a.score)[0];- }- function highlightElement(element, index, parentIframe = null) {- const container = createHighlightContainer(); // Now this will work- const color = COLORS[index % COLORS.length];- const pos = getAbsolutePosition(element, parentIframe);- const viewport = {- width: window.innerWidth,- height: window.innerHeight- };-- // Create highlight box- const box = document.createElement('div');- Object.assign(box.style, {- position: 'absolute',- border: `2px solid ${color}`,- backgroundColor: `${color}11`,- boxSizing: 'border-box',- pointerEvents: 'none',- top: `${pos.top}px`,- left: `${pos.left}px`,- width: `${pos.width}px`,- height: `${pos.height}px`- });+ overlay.style.top = `${top}px`;+ overlay.style.left = `${left}px`;+ overlay.style.width = `${rect.width}px`;+ overlay.style.height = `${rect.height}px`;// Create labelconst label = document.createElement('div');- const isSmallElement = pos.width < 30 || pos.height < 20;- Object.assign(label.style, {- position: 'absolute',- background: color,- color: 'white',- padding: isSmallElement ? '1px 3px' : '2px 4px',- borderRadius: '3px',- fontSize: isSmallElement ? '9px' : '10px',- lineHeight: '1',- whiteSpace: 'nowrap',- fontWeight: 'bold',- zIndex: 2147483647- });+ label.className = 'playwright-highlight-label';+ label.style.position = 'absolute';+ 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`; // Responsive font sizelabel.textContent = index;- const labelPos = findOptimalLabelPosition(pos, viewport);- Object.assign(label.style, {- top: `${labelPos.top}px`,- left: `${labelPos.left}px`- });+ // Calculate label position+ const labelWidth = 20; // Approximate width+ const labelHeight = 16; // Approximate height++ // Default position (top-right corner inside the box)+ let labelTop = top + 2;+ let labelLeft = left + rect.width - labelWidth - 2;++ // Adjust if box is too small+ if (rect.width < labelWidth + 4 || rect.height < labelHeight + 4) {+ // Position outside the box if it's too small+ labelTop = top - labelHeight - 2;+ labelLeft = left + rect.width - labelWidth;+ }++ // Ensure label stays within viewport+ if (labelTop < 0) labelTop = top + 2;+ if (labelLeft < 0) labelLeft = left + 2;+ if (labelLeft + labelWidth > window.innerWidth) {+ labelLeft = left + rect.width - labelWidth - 2;+ }- usedPositions.add(`${Math.round(labelPos.top)},${Math.round(labelPos.left)}`);+ label.style.top = `${labelTop}px`;+ label.style.left = `${labelLeft}px`;- container.appendChild(box);+ // Add to container+ container.appendChild(overlay);container.appendChild(label);+ // Store reference for cleanupelement.setAttribute('browser-user-highlight-id', `playwright-highlight-${index}`);++ return index + 1;}+// Helper function to generate XPath as a treefunction getXPathTree(element, stopAtBoundary = true) {const segments = [];@@ -267,7 +143,7 @@// Base interactive elements and rolesconst interactiveElements = new Set(['a', 'button', 'details', 'embed', 'input', 'label',- 'menu', 'menuitem', 'object', 'select', 'textarea', 'summary', 'th-tds-button-link'+ 'menu', 'menuitem', 'object', 'select', 'textarea', 'summary']);const interactiveRoles = new Set([