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
Remove viewportCoordinates - 3x speedup on amazon to 5sec
Magnus Müller9c502e6d3d2f
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.jsindex bed585733..d7190c36c 100644--- a/browser_use/dom/buildDomTree.js+++ b/browser_use/dom/buildDomTree.js@@ -145,7 +145,7 @@];const colorIndex = index % colors.length;const baseColor = colors[colorIndex];- const backgroundColor = `${baseColor}1A`; // 10% opacity version of the color+ const backgroundColor = baseColor + "1A"; // 10% opacity version of the color// Create highlight overlayconst overlay = document.createElement("div");@@ -235,17 +235,10 @@label.style.left = `${newLabelLeft}px`;};- // Add scroll listeners- let currentEl = element;- while (currentEl) {- if (currentEl.scrollHeight > currentEl.clientHeight ||- currentEl.scrollWidth > currentEl.clientWidth) {- currentEl.addEventListener('scroll', updatePositions);- }- currentEl = currentEl.parentElement;- }+window.addEventListener('scroll', updatePositions);+ window.addEventListener('resize', updatePositions);return index + 1;}@@ -547,29 +540,17 @@*/function isInExpandedViewport(element, viewportExpansion) {if (viewportExpansion === -1) {- return true;+ return true;}const rect = element.getBoundingClientRect();- const { scrollX, scrollY } = getEffectiveScroll(element); // Use effective scroll-- // 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 element position using effective scroll- const absTop = rect.top + scrollY;- const absLeft = rect.left + scrollX;- const absBottom = rect.bottom + scrollY;- const absRight = rect.right + scrollX;-++ // Simple viewport check without scroll calculationsreturn !(- absBottom < viewportTop ||- absTop > viewportBottom ||- absRight < viewportLeft ||- absLeft > viewportRight+ rect.bottom < -viewportExpansion ||+ rect.top > window.innerHeight + viewportExpansion ||+ rect.right < -viewportExpansion ||+ rect.left > window.innerWidth + viewportExpansion);}@@ -581,38 +562,23 @@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+ // Simple size check+ if (rect.width === 0 || rect.height === 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);- return (- rect.width !== 0 &&- rect.height !== 0 &&- isInExpandedViewport &&- textNode.parentElement?.checkVisibility({+ return isInViewport && textNode.parentElement?.checkVisibility({checkOpacity: true,checkVisibilityCSS: true,- })- );+ });}// Add this new helper function@@ -694,63 +660,11 @@if (node.nodeType === Node.ELEMENT_NODE) {measureBuildDomTreePart('attributeProcessing', () => {const rect = measureDomOperation(() => node.getBoundingClientRect(), 'getBoundingClientRect');- const { scrollX, scrollY } = getEffectiveScroll(node);-- nodeData.viewportCoordinates = {- topLeft: {- x: Math.round(rect.left),- y: Math.round(rect.top),- },- topRight: {- x: Math.round(rect.right),- y: Math.round(rect.top),- },- bottomLeft: {- x: Math.round(rect.left),- y: Math.round(rect.bottom),- },- bottomRight: {- x: Math.round(rect.right),- y: Math.round(rect.bottom),- },- center: {- x: Math.round(rect.left + rect.width / 2),- y: Math.round(rect.top + rect.height / 2),- },- width: Math.round(rect.width),- height: Math.round(rect.height),- };-- nodeData.pageCoordinates = {- topLeft: {- x: Math.round(rect.left + scrollX),- y: Math.round(rect.top + scrollY),- },- topRight: {- x: Math.round(rect.right + scrollX),- y: Math.round(rect.top + scrollY),- },- bottomLeft: {- x: Math.round(rect.left + scrollX),- y: Math.round(rect.bottom + scrollY),- },- bottomRight: {- x: Math.round(rect.right + scrollX),- y: Math.round(rect.bottom + scrollY),- },- center: {- x: Math.round(rect.left + rect.width / 2 + scrollX),- y: Math.round(rect.top + rect.height / 2 + scrollY),- },- width: Math.round(rect.width),- height: Math.round(rect.height),- };-++ // Only store minimal viewport infonodeData.viewport = {- scrollX: Math.round(scrollX),- scrollY: Math.round(scrollY),width: window.innerWidth,- height: window.innerHeight,+ height: window.innerHeight};});}diff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex 05a5b7a07..e89f26be0 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -1,27 +1,31 @@import gcimport jsonimport logging+from dataclasses import dataclassfrom importlib import resourcesfrom typing import TYPE_CHECKING, Optionalif TYPE_CHECKING:from playwright.async_api import Page-from browser_use.dom.history_tree_processor.view import Coordinatesfrom browser_use.dom.views import (- CoordinateSet,DOMBaseNode,DOMElementNode,DOMState,DOMTextNode,SelectorMap,- ViewportInfo,)from browser_use.utils import time_execution_asynclogger = logging.getLogger(__name__)+@dataclass+class ViewportInfo:+ width: int+ height: int++class DomService:def __init__(self, page: 'Page'):self.page = page@@ -134,34 +138,11 @@ class DomService:return text_node, []# Process coordinates if they exist for element nodes- viewport_coordinates = None- page_coordinates = None+viewport_info = None- if 'viewportCoordinates' in node_data:- viewport_coordinates = CoordinateSet(- top_left=Coordinates(**node_data['viewportCoordinates']['topLeft']),- top_right=Coordinates(**node_data['viewportCoordinates']['topRight']),- bottom_left=Coordinates(**node_data['viewportCoordinates']['bottomLeft']),- bottom_right=Coordinates(**node_data['viewportCoordinates']['bottomRight']),- center=Coordinates(**node_data['viewportCoordinates']['center']),- width=node_data['viewportCoordinates']['width'],- height=node_data['viewportCoordinates']['height'],- )- if 'pageCoordinates' in node_data:- page_coordinates = CoordinateSet(- top_left=Coordinates(**node_data['pageCoordinates']['topLeft']),- top_right=Coordinates(**node_data['pageCoordinates']['topRight']),- bottom_left=Coordinates(**node_data['pageCoordinates']['bottomLeft']),- bottom_right=Coordinates(**node_data['pageCoordinates']['bottomRight']),- center=Coordinates(**node_data['pageCoordinates']['center']),- width=node_data['pageCoordinates']['width'],- height=node_data['pageCoordinates']['height'],- )if 'viewport' in node_data:viewport_info = ViewportInfo(- scroll_x=node_data['viewport']['scrollX'],- scroll_y=node_data['viewport']['scrollY'],width=node_data['viewport']['width'],height=node_data['viewport']['height'],)@@ -178,8 +159,6 @@ class DomService:highlight_index=node_data.get('highlightIndex'),shadow_root=node_data.get('shadowRoot', False),parent=None,- viewport_coordinates=viewport_coordinates,- page_coordinates=page_coordinates,viewport_info=viewport_info,)