Skip to content

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

Include in viewport parameter

Magnus Müller4ec5fe437809
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index 649aa9d90..bad5248fa 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -15,6 +15,7 @@ from browser_use.dom.views import (
SelectorMap,
ViewportInfo,
)
+from browser_use.utils import time_execution_async
logger = logging.getLogger(__name__)
@@ -27,7 +28,7 @@ class DomService:
self.js_code = resources.read_text('browser_use.dom', 'buildDomTree.js')
# region - Clickable elements
-
+ @time_execution_async('--get_clickable_elements')
async def get_clickable_elements(
self,
highlight_elements: bool = True,
@@ -40,6 +41,7 @@ class DomService:
return dom_state
+ @time_execution_async('--build_dom_tree')
async def _build_dom_tree(
self,
highlight_elements: bool,
@@ -158,6 +160,7 @@ class DomService:
is_visible=node_data.get('isVisible', False),
is_interactive=node_data.get('isInteractive', False),
is_top_element=node_data.get('isTopElement', False),
+ is_in_viewport=node_data.get('isInViewport', False),
highlight_index=node_data.get('highlightIndex'),
shadow_root=node_data.get('shadowRoot', False),
parent=None,
diff --git a/browser_use/dom/views.py b/browser_use/dom/views.py
index 67d2a4e45..6345f99c9 100644
--- a/browser_use/dom/views.py
+++ b/browser_use/dom/views.py
@@ -26,6 +26,10 @@ class DOMTextNode(DOMBaseNode):
while current is not None:
if current.highlight_index is not None:
return True
+ if not current.is_top_element:
+ return True
+ if not current.is_in_viewport:
+ return True
current = current.parent
return False
@@ -43,6 +47,7 @@ class DOMElementNode(DOMBaseNode):
children: List[DOMBaseNode]
is_interactive: bool = False
is_top_element: bool = False
+ is_in_viewport: bool = False
shadow_root: bool = False
highlight_index: Optional[int] = None
viewport_coordinates: Optional[CoordinateSet] = None
@@ -67,6 +72,8 @@ class DOMElementNode(DOMBaseNode):
extras.append('shadow-root')
if self.highlight_index is not None:
extras.append(f'highlight:{self.highlight_index}')
+ if self.is_in_viewport:
+ extras.append('in-viewport')
if extras:
tag_str += f' [{", ".join(extras)}]'