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
vibe coded serializer
Gregor Žunič1a24cf229f98
diff --git a/browser_use/dom/serializer/serializer.py b/browser_use/dom/serializer/serializer.pyindex 14c40bbbd..2354dccbc 100644--- a/browser_use/dom/serializer/serializer.py+++ b/browser_use/dom/serializer/serializer.py@@ -1,5 +1,7 @@# @file purpose: Serializes enhanced DOM trees to string format for LLM consumption+from dataclasses import dataclass, field+from enum import Enumfrom cdp_use.cdp.accessibility.types import AXPropertyName@@ -7,21 +9,330 @@ from browser_use.dom.utils import cap_text_lengthfrom browser_use.dom.views import DOMSelectorMap, EnhancedDOMTreeNode, NodeType, SerializedDOMState, SimplifiedNode-class ClickableElementDetector:- @staticmethod- def is_interactive(node: EnhancedDOMTreeNode) -> bool:- """Check if this node is clickable/interactive."""- has_focusable_property = (- any(property.name == AXPropertyName.FOCUSABLE and property.value for property in node.ax_node.properties)- if node.ax_node and node.ax_node.properties- else False+class ElementGroup(Enum):+ """Types of element groups for semantic organization."""++ FORM = 'FORM'+ NAVIGATION = 'NAVIGATION'+ DROPDOWN = 'DROPDOWN'+ MENU = 'MENU'+ TABLE = 'TABLE'+ LIST = 'LIST'+ TABS = 'TABS'+ CONTENT = 'CONTENT'+++@dataclass+class ElementAnalysis:+ """Analysis of element interactivity with scoring."""++ score: int+ confidence: str+ primary_reason: str+ element_type: str+ evidence: list[str] = field(default_factory=list)+ warnings: list[str] = field(default_factory=list)+ interactive_indicators: dict[str, bool] = field(default_factory=dict)++ @classmethod+ def analyze_element_interactivity(cls, node: EnhancedDOMTreeNode) -> 'ElementAnalysis':+ """Analyze element interactivity with comprehensive scoring."""+ element_name = node.node_name.upper()+ attributes = node.attributes or {}++ score = 0+ evidence = []+ warnings = []+ element_category = 'unknown'++ # Enhanced button detection indicators+ button_indicators = [+ 'btn',+ 'button',+ 'click',+ 'submit',+ 'action',+ 'trigger',+ 'toggle',+ 'press',+ 'tap',+ 'select',+ 'choose',+ 'confirm',+ 'cancel',+ 'ok',+ 'yes',+ 'no',+ 'close',+ 'open',+ 'show',+ 'hide',+ 'expand',+ 'collapse',+ 'menu',+ 'dropdown',+ 'popup',+ 'modal',+ 'dialog',+ 'tab',+ 'nav',+ 'link',+ 'item',+ 'option',+ 'choice',+ ]++ # TIER 1: HIGHEST PRIORITY (90-100 points) - Core interactive elements+ if element_name in ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA']:+ element_category = 'form_control'+ score += 90+ evidence.append(f'HIGH PRIORITY: Core form element: {element_name}')++ if attributes.get('type'):+ input_type = attributes['type'].lower()+ evidence.append(f'Input type: {input_type}')+ if input_type in ['submit', 'button', 'reset']:+ score += 10+ elif input_type in ['text', 'email', 'password', 'search', 'tel', 'url']:+ score += 8+ elif input_type in ['checkbox', 'radio']:+ score += 6++ if attributes.get('disabled') == 'true':+ score = max(25, score - 40)+ warnings.append('Element is disabled but still detectable')++ # TIER 2: VERY HIGH PRIORITY (80-89 points) - Cursor pointer + interactive elements+ elif cls._has_cursor_pointer(node):+ element_category = 'cursor_pointer'+ score += 80+ evidence.append('VERY HIGH PRIORITY: Element has cursor: pointer')++ if element_name in ['DIV', 'SPAN', 'A', 'LI', 'TD', 'TH']:+ score += 5+ evidence.append('Meaningful element with cursor pointer')++ # TIER 3: HIGH PRIORITY (70-79 points) - Links and strong event indicators+ elif element_name == 'A':+ element_category = 'link'+ score += 70+ if attributes.get('href'):+ href = attributes['href']+ score += 10+ evidence.append(f'HIGH PRIORITY: Link with href: {href[:50]}...' if len(href) > 50 else f'Link with href: {href}')++ if href.startswith(('http://', 'https://')):+ score += 5+ evidence.append('External link')+ elif href.startswith('/'):+ score += 4+ evidence.append('Internal absolute link')+ else:+ score += 8+ evidence.append('Link element without href (likely interactive)')++ # ENHANCED ONCLICK DETECTION - TIER 3 priority+ elif 'onclick' in attributes:+ element_category = 'onclick_handler'+ score += 75+ evidence.append('HIGH PRIORITY: Has onclick event handler')++ # TIER 4: MEDIUM-HIGH PRIORITY (50-69 points) - ARIA roles and containers+ elif attributes.get('role'):+ if element_category == 'unknown':+ element_category = 'aria_role'+ role = attributes['role'].lower()+ interactive_roles = {+ 'button': 65,+ 'link': 65,+ 'menuitem': 60,+ 'tab': 60,+ 'option': 55,+ 'checkbox': 55,+ 'radio': 55,+ 'switch': 55,+ 'slider': 50,+ 'spinbutton': 50,+ 'combobox': 50,+ 'textbox': 50,+ }++ if role in interactive_roles:+ role_score = interactive_roles[role]+ score += role_score+ evidence.append(f'MEDIUM-HIGH PRIORITY: ARIA role: {role} (+{role_score})')++ # ENHANCED BUTTON DETECTION IN CONTAINERS - TIER 4-5 priority+ elif element_name in ['DIV', 'SPAN', 'LI', 'TD', 'TH', 'SECTION', 'ARTICLE']:+ if element_category == 'unknown':+ element_category = 'container'++ container_score = cls._analyze_container_interactivity(node, attributes, button_indicators, evidence)+ score += container_score++ # ENHANCED TABINDEX ANALYSIS+ if 'tabindex' in attributes:+ try:+ tabindex = int(attributes['tabindex'])+ if tabindex >= 0:+ score += 25+ evidence.append(f'ENHANCED: Focusable (tabindex: {tabindex}) (+25)')+ elif tabindex == -1:+ score += 15+ evidence.append('Programmatically focusable (tabindex: -1) (+15)')+ except ValueError:+ warnings.append(f'Invalid tabindex: {attributes["tabindex"]}')++ # ACCESSIBILITY TREE ENHANCEMENTS+ accessibility_boost = cls._analyze_accessibility_properties(node, evidence)+ score += accessibility_boost++ # DETERMINE FINAL CONFIDENCE+ if score >= 85:+ confidence = 'DEFINITE'+ elif score >= 65:+ confidence = 'LIKELY'+ elif score >= 40:+ confidence = 'POSSIBLE'+ elif score >= 20:+ confidence = 'QUESTIONABLE'+ else:+ confidence = 'MINIMAL'++ primary_reason = element_category if element_category != 'unknown' else 'mixed_indicators'++ # ENHANCED INTERACTIVE INDICATORS+ interactive_indicators = {+ 'has_onclick': 'onclick' in attributes,+ 'has_href': 'href' in attributes,+ 'has_tabindex': 'tabindex' in attributes,+ 'has_role': 'role' in attributes,+ 'has_aria_label': 'aria-label' in attributes,+ 'has_data_attrs': any(k.startswith('data-') for k in attributes.keys()),+ 'has_button_classes': any(indicator in attributes.get('class', '').lower() for indicator in button_indicators),+ 'has_pointer_cursor': cls._has_cursor_pointer(node),+ 'has_event_handlers': any(k.startswith('on') for k in attributes.keys()),+ 'is_focusable': cls._is_ax_focusable(node),+ }++ return cls(+ score=score,+ confidence=confidence,+ primary_reason=primary_reason,+ element_type=element_name,+ evidence=evidence,+ warnings=warnings,+ interactive_indicators=interactive_indicators,)- cursor_pointer = node.snapshot_node and node.snapshot_node.cursor_style == 'pointer'+ @classmethod+ def _has_cursor_pointer(cls, node: EnhancedDOMTreeNode) -> bool:+ """Enhanced cursor pointer detection."""+ if node.snapshot_node:+ if getattr(node.snapshot_node, 'cursor_style', None) == 'pointer':+ return True+ if hasattr(node.snapshot_node, 'computed_styles'):+ styles = node.snapshot_node.computed_styles or {}+ if styles.get('cursor') == 'pointer':+ return True+ return False++ @classmethod+ def _analyze_container_interactivity(+ cls, node: EnhancedDOMTreeNode, attributes: dict[str, str], button_indicators: list[str], evidence: list[str]+ ) -> int:+ """Analyze container elements for interactivity."""+ container_score = 10++ # Enhanced CSS class analysis+ css_classes = attributes.get('class', '').lower()+ button_score_boost = 0+ for indicator in button_indicators:+ if indicator in css_classes:+ button_score_boost += 20+ evidence.append(f'Button-like class: {indicator}')++ container_score += button_score_boost++ # Check for cursor pointer+ if cls._has_cursor_pointer(node):+ container_score += 50+ evidence.append('Container with cursor pointer (+50)')++ # Enhanced ARIA attribute detection+ interactive_aria = [+ 'aria-label',+ 'aria-expanded',+ 'aria-selected',+ 'aria-pressed',+ 'aria-checked',+ 'aria-controls',+ 'aria-haspopup',+ 'aria-live',+ 'aria-hidden',+ ]+ found_aria = [attr for attr in interactive_aria if attr in attributes]+ if found_aria:+ aria_boost = len(found_aria) * 8+ container_score += aria_boost+ evidence.append(f'Interactive ARIA attributes: {", ".join(found_aria)} (+{aria_boost})')++ return container_score++ @classmethod+ def _analyze_accessibility_properties(cls, node: EnhancedDOMTreeNode, evidence: list[str]) -> int:+ """Analyze accessibility properties for enhanced scoring."""+ score_boost = 0++ if cls._is_ax_focusable(node):+ score_boost += 30+ evidence.append('Accessibility tree marked as focusable (+30)')++ if node.ax_node and node.ax_node.role:+ role = node.ax_node.role.lower()+ interactive_ax_roles = {+ 'button': 25,+ 'link': 25,+ 'menuitem': 20,+ 'tab': 20,+ 'checkbox': 20,+ 'radio': 20,+ 'slider': 15,+ 'textbox': 15,+ }+ if role in interactive_ax_roles:+ boost = interactive_ax_roles[role]+ score_boost += boost+ evidence.append(f'Interactive AX role: {role} (+{boost})')++ return score_boost++ @classmethod+ def _is_ax_focusable(cls, node: EnhancedDOMTreeNode) -> bool:+ """Check if element is focusable according to accessibility tree."""+ if node.ax_node and node.ax_node.properties:+ for prop in node.ax_node.properties:+ if prop.name == AXPropertyName.FOCUSABLE and prop.value:+ return True+ return False+++@dataclass+class SemanticGroup:+ """A semantic group of related elements."""++ group_type: ElementGroup+ elements: list['SimplifiedNode'] = field(default_factory=list)+ title: str | None = None- is_clickable = node.snapshot_node and node.snapshot_node.is_clickable or has_focusable_property or cursor_pointer- return is_clickable or False+class ClickableElementDetector:+ @staticmethod+ def is_interactive(node: EnhancedDOMTreeNode) -> bool:+ """Check if this node is clickable/interactive using enhanced scoring."""+ analysis = ElementAnalysis.analyze_element_interactivity(node)+ # Use a threshold of 20 points for interactivity+ return analysis.score >= 50class DOMTreeSerializer:@@ -29,16 +340,16 @@ class DOMTreeSerializer:def __init__(self, root_node: EnhancedDOMTreeNode, previous_cached_state: SerializedDOMState | None = None):self.root_node = root_node-self._interactive_counter = 1self._selector_map: DOMSelectorMap = {}-self._previous_cached_selector_map = previous_cached_state.selector_map if previous_cached_state else None+ self._semantic_groups: list[SemanticGroup] = []def serialize_accessible_elements(self) -> SerializedDOMState:# Reset stateself._interactive_counter = 1self._selector_map = {}+ self._semantic_groups = []# Step 1: Create simplified treesimplified_tree = self._create_simplified_tree(self.root_node)@@ -46,16 +357,18 @@ class DOMTreeSerializer:# Step 2: Optimize tree (remove unnecessary parents)optimized_tree = self._optimize_tree(simplified_tree)- # Step 3: Assign interactive indices to clickable elements+ # Step 3: Detect and group semantic elements+ if optimized_tree:+ self._detect_semantic_groups(optimized_tree)++ # Step 4: Assign interactive indices to clickable elementsself._assign_interactive_indices_and_mark_new_nodes(optimized_tree)return SerializedDOMState(_root=optimized_tree, selector_map=self._selector_map)def _create_simplified_tree(self, node: EnhancedDOMTreeNode) -> SimplifiedNode | None:- """Step 1: Create a simplified tree with relevant elements."""-+ """Step 1: Create a simplified tree with enhanced element detection."""if node.node_type == NodeType.DOCUMENT_NODE:- # Document nodes - process children directly and return the first meaningful childif node.children_nodes:for child in node.children_nodes:simplified_child = self._create_simplified_tree(child)@@ -64,7 +377,6 @@ class DOMTreeSerializer:return Noneelif node.node_type == NodeType.ELEMENT_NODE:- # Skip #document nodes entirely - process children directlyif node.node_name == '#document':if node.children_nodes:for child in node.children_nodes:@@ -73,31 +385,22 @@ class DOMTreeSerializer:return simplified_childreturn None- # Skip elements that contain non-content+ # Skip non-content elementsif node.node_name.lower() in ['style', 'script', 'head', 'meta', 'link', 'title']:return None- # Be more inclusive - include most elements and let optimization handle filtering- # Include if: clickable, has AX data (non-ignored), or structural element, or has meaningful attributes-- has_focusable_property = (- any(property.name == AXPropertyName.FOCUSABLE and property.value for property in node.ax_node.properties)- if node.ax_node and node.ax_node.properties- else False- )-- cursor_pointer = node.snapshot_node and node.snapshot_node.cursor_style == 'pointer'-- is_clickable = node.snapshot_node and node.snapshot_node.is_clickable or has_focusable_property or cursor_pointer-+ # Use enhanced scoring for inclusion decision+ analysis = ElementAnalysis.analyze_element_interactivity(node)+ is_interactive = analysis.score >= 20is_visible = node.snapshot_node and node.snapshot_node.is_visibleis_scrollable = node.is_scrollable- # Simple criteria: include if (clickable AND visible) OR scrollable- should_include = (is_clickable and is_visible) or is_scrollable+ # Include if interactive and visible, or scrollable, or has children to process+ should_include = (is_interactive and is_visible) or is_scrollable or node.children_nodes- if should_include or node.children_nodes: # Include if meaningful OR has children to process+ if should_include:simplified = SimplifiedNode(original_node=node)+ # simplified._analysis = analysis # Store analysis for grouping# Process childrenif node.children_nodes:@@ -106,37 +409,33 @@ class DOMTreeSerializer:if simplified_child:simplified.children.append(simplified_child)- # Only return this node if it's meaningful OR has meaningful children- if should_include or simplified.children:+ # Return if meaningful or has meaningful children+ if (is_interactive and is_visible) or is_scrollable or simplified.children:return simplified- return Noneelif node.node_type == NodeType.TEXT_NODE:- # Include text nodes only if visible+ # Include meaningful text nodesis_visible = node.snapshot_node and node.snapshot_node.is_visibleif is_visible and node.node_value and node.node_value.strip() and len(node.node_value.strip()) > 1:return SimplifiedNode(original_node=node)- return None- # Skip other node types (COMMENT_NODE, DOCUMENT_TYPE_NODE, etc.)return Nonedef _optimize_tree(self, node: SimplifiedNode | None) -> SimplifiedNode | None:- """Step 2: Simple optimization - just process children and keep meaningful nodes."""+ """Step 2: Optimize tree structure."""if not node:return None- # Process all children+ # Process childrenoptimized_children = []for child in node.children:optimized_child = self._optimize_tree(child)if optimized_child:optimized_children.append(optimized_child)- # Update children with optimized versionsnode.children = optimized_children- # Keep the node if it's meaningful or has meaningful children+ # Keep meaningful nodesif (ClickableElementDetector.is_interactive(node.original_node)or node.original_node.is_scrollable@@ -145,9 +444,97 @@ class DOMTreeSerializer:):return node- # Remove empty nodesreturn None+ def _detect_semantic_groups(self, node: SimplifiedNode) -> None:+ """Step 3: Detect semantic groups of related elements."""+ self._semantic_groups = []++ # Collect all interactive elements+ interactive_elements = []+ self._collect_interactive_elements(node, interactive_elements)++ # Group by semantic type+ form_elements = []+ nav_elements = []+ dropdown_elements = []+ list_elements = []+ tab_elements = []+ other_elements = []++ for element in interactive_elements:+ group_type = self._determine_element_group_type(element)+ if group_type == ElementGroup.FORM:+ form_elements.append(element)+ elif group_type == ElementGroup.NAVIGATION:+ nav_elements.append(element)+ elif group_type == ElementGroup.DROPDOWN:+ dropdown_elements.append(element)+ elif group_type == ElementGroup.LIST:+ list_elements.append(element)+ elif group_type == ElementGroup.TABS:+ tab_elements.append(element)+ else:+ other_elements.append(element)++ # Create semantic groups+ if form_elements:+ self._semantic_groups.append(SemanticGroup(ElementGroup.FORM, form_elements, 'Form Elements'))+ if nav_elements:+ self._semantic_groups.append(SemanticGroup(ElementGroup.NAVIGATION, nav_elements, 'Navigation'))+ if dropdown_elements:+ self._semantic_groups.append(SemanticGroup(ElementGroup.DROPDOWN, dropdown_elements, 'Dropdown/Menu'))+ if list_elements:+ self._semantic_groups.append(SemanticGroup(ElementGroup.LIST, list_elements, 'List Items'))+ if tab_elements:+ self._semantic_groups.append(SemanticGroup(ElementGroup.TABS, tab_elements, 'Tabs'))+ if other_elements:+ self._semantic_groups.append(SemanticGroup(ElementGroup.CONTENT, other_elements, 'Interactive Content'))++ def _collect_interactive_elements(self, node: SimplifiedNode, elements: list[SimplifiedNode]) -> None:+ """Recursively collect interactive elements."""+ if ClickableElementDetector.is_interactive(node.original_node):+ elements.append(node)++ for child in node.children:+ self._collect_interactive_elements(child, elements)++ def _determine_element_group_type(self, element: SimplifiedNode) -> ElementGroup:+ """Determine the semantic group type for an element."""+ node = element.original_node+ node_name = node.node_name.upper()+ attrs = node.attributes or {}++ # Form elements+ if node_name in ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA', 'FORM']:+ return ElementGroup.FORM++ # Navigation elements+ if node_name == 'A' and attrs.get('href'):+ href = attrs['href']+ if not href.startswith(('mailto:', 'tel:', 'javascript:', '#')):+ return ElementGroup.NAVIGATION++ # Check for semantic indicators in classes/roles+ classes = attrs.get('class', '').lower()+ role = attrs.get('role', '').lower()++ # Dropdown/Menu indicators+ if any(indicator in classes for indicator in ['dropdown', 'menu', 'select']):+ return ElementGroup.DROPDOWN+ if role in ['menu', 'menuitem', 'option']:+ return ElementGroup.DROPDOWN++ # List indicators+ if node_name in ['LI', 'UL', 'OL'] or 'list' in classes:+ return ElementGroup.LIST++ # Tab indicators+ if 'tab' in classes or role == 'tab':+ return ElementGroup.TABS++ return ElementGroup.CONTENT+def _assign_interactive_indices_and_mark_new_nodes(self, node: SimplifiedNode | None) -> None:"""Assign interactive indices to clickable elements."""if not node:@@ -159,13 +546,9 @@ class DOMTreeSerializer:self._selector_map[self._interactive_counter] = node.original_nodeself._interactive_counter += 1- # If we provided previous cached selector map, check if the node is new- # TODO: maybe we can also check for more than just 1 step- previous_backend_node_ids: set[int] | None = None+ # Check if node is newif self._previous_cached_selector_map:previous_backend_node_ids = {node.backend_node_id for node in self._previous_cached_selector_map.values()}-- if previous_backend_node_ids:if node.original_node.backend_node_id not in previous_backend_node_ids:node.is_new = True@@ -184,7 +567,7 @@ class DOMTreeSerializer:next_depth = depthif node.original_node.node_type == NodeType.ELEMENT_NODE:- # Skip displaying nodes marked as should_display=False (virtual nodes)+ # Skip displaying nodes marked as should_display=Falseif not node.should_display:for child in node.children:child_text = DOMTreeSerializer.serialize_tree(child, include_attributes, depth)@@ -202,14 +585,14 @@ class DOMTreeSerializer:# Build the lineif node.original_node.is_scrollable and node.interactive_index is None:# Scrollable but not clickable- line = f'{depth_str}|SCROLL|<{node.original_node.node_name}'+ 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 '['- line = f'{depth_str}{new_prefix}{scroll_prefix}{node.interactive_index}]<{node.original_node.node_name}'+ line = f'{depth_str}{new_prefix}{scroll_prefix}{node.interactive_index}]<{node.original_node.tag_name}'else:- line = f'{depth_str}<{node.original_node.node_name}'+ line = f'{depth_str}<{node.original_node.tag_name}'if attributes_html_str:line += f' {attributes_html_str}'@@ -218,7 +601,7 @@ class DOMTreeSerializer:formatted_text.append(line)elif node.original_node.node_type == NodeType.TEXT_NODE:- # Include all visible text where it naturally appears+ # Include visible textis_visible = node.original_node.snapshot_node and node.original_node.snapshot_node.is_visibleif (is_visible