Skip to content

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

Scroll info works

Magnus Müllerd086e77ecbf9
diff --git a/browser_use/dom/serializer/serializer.py b/browser_use/dom/serializer/serializer.py
index b3a749668..41e1df012 100644
--- a/browser_use/dom/serializer/serializer.py
+++ b/browser_use/dom/serializer/serializer.py
@@ -115,7 +115,7 @@ class DOMTreeSerializer:
is_interactive = self._is_interactive_cached(node)
is_visible = node.snapshot_node and node.is_visible
- is_scrollable = node.is_scrollable
+ is_scrollable = node.is_actually_scrollable
# Include if interactive (regardless of visibility), or scrollable, or has children to process
should_include = (is_interactive and is_visible) or is_scrollable or node.children_and_shadow_roots
@@ -162,7 +162,7 @@ class DOMTreeSerializer:
if (
(is_interactive_opt and is_visible) # Only keep interactive nodes that are visible
- or node.original_node.is_scrollable
+ or node.original_node.is_actually_scrollable
or node.original_node.node_type == NodeType.TEXT_NODE
or node.children
):
@@ -228,24 +228,21 @@ class DOMTreeSerializer:
return '\n'.join(formatted_text)
# Add element with interactive_index if clickable, scrollable, or iframe
- if (
- node.interactive_index is not None
- or node.original_node.is_scrollable
- or node.original_node.tag_name.upper() == 'IFRAME'
- ):
+ is_any_scrollable = node.original_node.is_actually_scrollable
+ if node.interactive_index is not None or is_any_scrollable or node.original_node.tag_name.upper() == 'IFRAME':
next_depth += 1
# Build attributes string
attributes_html_str = DOMTreeSerializer._build_attributes_string(node.original_node, include_attributes, '')
# Build the line
- if node.original_node.is_scrollable and node.interactive_index is None:
+ if is_any_scrollable and node.interactive_index is None:
# Scrollable but not clickable
line = f'{depth_str}|SCROLL|<{node.original_node.tag_name}'
elif node.interactive_index is not None:
# Clickable (and possibly scrollable)
new_prefix = '*' if node.is_new else ''
- scroll_prefix = '|SCROLL+' if node.original_node.is_scrollable else '['
+ scroll_prefix = '|SCROLL+' if is_any_scrollable else '['
line = f'{depth_str}{new_prefix}{scroll_prefix}{node.interactive_index}]<{node.original_node.tag_name}'
elif node.original_node.tag_name.upper() == 'IFRAME':
# Iframe element (not interactive)
@@ -257,6 +254,13 @@ class DOMTreeSerializer:
line += f' {attributes_html_str}'
line += ' />'
+
+ # Add scroll information for scrollable elements
+ if is_any_scrollable:
+ scroll_info_text = node.original_node.get_scroll_info_text()
+ if scroll_info_text:
+ line += f' ({scroll_info_text})'
+
formatted_text.append(line)
elif node.original_node.node_type == NodeType.TEXT_NODE:
diff --git a/browser_use/dom/views.py b/browser_use/dom/views.py
index 2e6e778ba..0b1bc01e4 100644
--- a/browser_use/dom/views.py
+++ b/browser_use/dom/views.py
@@ -381,6 +381,162 @@ class EnhancedDOMTreeNode:
return f'<{self.tag_name}>{cap_text_length(self.get_all_children_text(), max_text_length) or ""}'
+ @property
+ def is_actually_scrollable(self) -> bool:
+ """
+ Enhanced scroll detection that combines CDP detection with CSS analysis.
+
+ This detects scrollable elements that Chrome's CDP might miss, which is common
+ in iframes and dynamically sized containers.
+ """
+ # First check if CDP already detected it as scrollable
+ if self.is_scrollable:
+ return True
+
+ # Enhanced detection for elements CDP missed
+ if not self.snapshot_node:
+ return False
+
+ # Check scroll vs client rects - this is the most reliable indicator
+ scroll_rects = self.snapshot_node.scrollRects
+ client_rects = self.snapshot_node.clientRects
+
+ if scroll_rects and client_rects:
+ # Content is larger than visible area = scrollable
+ has_vertical_scroll = scroll_rects.height > client_rects.height + 1 # +1 for rounding
+ has_horizontal_scroll = scroll_rects.width > client_rects.width + 1
+
+ if has_vertical_scroll or has_horizontal_scroll:
+ # Also check CSS to make sure scrolling is allowed
+ if self.snapshot_node.computed_styles:
+ styles = self.snapshot_node.computed_styles
+
+ overflow = styles.get('overflow', 'visible').lower()
+ overflow_x = styles.get('overflow-x', overflow).lower()
+ overflow_y = styles.get('overflow-y', overflow).lower()
+
+ # Allow scrolling if overflow is not explicitly hidden
+ allows_scroll = (
+ overflow in ['auto', 'scroll', 'overlay']
+ or overflow_x in ['auto', 'scroll', 'overlay']
+ or overflow_y in ['auto', 'scroll', 'overlay']
+ or (overflow == 'visible' and (overflow_x != 'hidden' or overflow_y != 'hidden'))
+ )
+
+ return allows_scroll
+ else:
+ # No CSS info, but content overflows - likely scrollable
+ return True
+
+ return False
+
+ @property
+ def scroll_info(self) -> dict[str, Any] | None:
+ """Calculate scroll information for this element if it's scrollable."""
+ if not self.is_actually_scrollable or not self.snapshot_node:
+ return None
+
+ # Get scroll and client rects from snapshot data
+ scroll_rects = self.snapshot_node.scrollRects
+ client_rects = self.snapshot_node.clientRects
+ bounds = self.snapshot_node.bounds
+
+ if not scroll_rects or not client_rects:
+ return None
+
+ # Calculate scroll position and percentages
+ scroll_top = scroll_rects.y
+ scroll_left = scroll_rects.x
+
+ # Total scrollable height and width
+ scrollable_height = scroll_rects.height
+ scrollable_width = scroll_rects.width
+
+ # Visible (client) dimensions
+ visible_height = client_rects.height
+ visible_width = client_rects.width
+
+ # Calculate how much content is above/below/left/right of current view
+ content_above = max(0, scroll_top)
+ content_below = max(0, scrollable_height - visible_height - scroll_top)
+ content_left = max(0, scroll_left)
+ content_right = max(0, scrollable_width - visible_width - scroll_left)
+
+ # Calculate scroll percentages
+ vertical_scroll_percentage = 0
+ horizontal_scroll_percentage = 0
+
+ if scrollable_height > visible_height:
+ max_scroll_top = scrollable_height - visible_height
+ vertical_scroll_percentage = (scroll_top / max_scroll_top) * 100 if max_scroll_top > 0 else 0
+
+ if scrollable_width > visible_width:
+ max_scroll_left = scrollable_width - visible_width
+ horizontal_scroll_percentage = (scroll_left / max_scroll_left) * 100 if max_scroll_left > 0 else 0
+
+ # Calculate pages equivalent (using visible height as page unit)
+ pages_above = content_above / visible_height if visible_height > 0 else 0
+ pages_below = content_below / visible_height if visible_height > 0 else 0
+ total_pages = scrollable_height / visible_height if visible_height > 0 else 1
+
+ return {
+ 'scroll_top': scroll_top,
+ 'scroll_left': scroll_left,
+ 'scrollable_height': scrollable_height,
+ 'scrollable_width': scrollable_width,
+ 'visible_height': visible_height,
+ 'visible_width': visible_width,
+ 'content_above': content_above,
+ 'content_below': content_below,
+ 'content_left': content_left,
+ 'content_right': content_right,
+ 'vertical_scroll_percentage': round(vertical_scroll_percentage, 1),
+ 'horizontal_scroll_percentage': round(horizontal_scroll_percentage, 1),
+ 'pages_above': round(pages_above, 1),
+ 'pages_below': round(pages_below, 1),
+ 'total_pages': round(total_pages, 1),
+ 'can_scroll_up': content_above > 0,
+ 'can_scroll_down': content_below > 0,
+ 'can_scroll_left': content_left > 0,
+ 'can_scroll_right': content_right > 0,
+ }
+
+ def get_scroll_info_text(self) -> str:
+ """Get human-readable scroll information text for this element."""
+ scroll_info = self.scroll_info
+ if not scroll_info:
+ return ''
+
+ parts = []
+
+ # Vertical scroll info
+ if scroll_info['scrollable_height'] > scroll_info['visible_height']:
+ parts.append(
+ f'scroll: {scroll_info["pages_above"]:.1f} pages above, '
+ f'{scroll_info["pages_below"]:.1f} pages below, '
+ f'at {scroll_info["vertical_scroll_percentage"]:.0f}% vertically'
+ )
+
+ # Horizontal scroll info (if significant)
+ if scroll_info['scrollable_width'] > scroll_info['visible_width']:
+ parts.append(f'horizontal scroll: at {scroll_info["horizontal_scroll_percentage"]:.0f}%')
+
+ # Can scroll directions
+ scroll_directions = []
+ if scroll_info['can_scroll_up']:
+ scroll_directions.append('↑up')
+ if scroll_info['can_scroll_down']:
+ scroll_directions.append('↓down')
+ if scroll_info['can_scroll_left']:
+ scroll_directions.append('←left')
+ if scroll_info['can_scroll_right']:
+ scroll_directions.append('→right')
+
+ if scroll_directions:
+ parts.append(f'can scroll: {", ".join(scroll_directions)}')
+
+ return ' | '.join(parts)
+
@property
def element_hash(self) -> int:
return hash(self)