Skip to content

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

fix: include shadow DOM form elements in selector_map even without snapshot data

Saurav Panda07b7a6d8c0ba
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 8f0a5a297..fdd6c4c0e 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -2984,11 +2984,23 @@ class Agent(Generic[Context, AgentStructuredOutput]):
highlight_index: int | None = None
match_level: MatchLevel | None = None
- # Debug: log what we're looking for
- self.logger.debug(
- f'Searching for element: <{historical_element.node_name}> '
+ # Debug: log what we're looking for and what's available
+ self.logger.info(
+ f'πŸ” Searching for element: <{historical_element.node_name}> '
f'hash={historical_element.element_hash} stable_hash={historical_element.stable_hash}'
)
+ # Log what elements are in selector_map for debugging
+ if historical_element.node_name:
+ hist_name = historical_element.node_name.lower()
+ matching_nodes = [
+ (idx, elem.node_name, elem.attributes.get('name') if elem.attributes else None)
+ for idx, elem in selector_map.items()
+ if elem.node_name.lower() == hist_name
+ ]
+ self.logger.info(
+ f'πŸ” Selector map has {len(selector_map)} elements, '
+ f'{len(matching_nodes)} are <{hist_name.upper()}>: {matching_nodes}'
+ )
# Level 1: EXACT hash match
for idx, elem in selector_map.items():
@@ -3054,8 +3066,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):
for idx, elem in selector_map.items()
if elem.node_name.lower() == hist_name and elem.attributes
]
- self.logger.debug(
- f'ATTRIBUTE match failed for <{hist_name.upper()}> '
+ self.logger.info(
+ f'πŸ” ATTRIBUTE match failed for <{hist_name.upper()}> '
f'(tried: {tried_attrs}, looking for: {[hist_attrs.get(k) for k in tried_attrs]}). '
f'Page has {len(same_node_elements)} <{hist_name.upper()}> elements with identifiers: '
f'{same_node_elements[:5]}{"..." if len(same_node_elements) > 5 else ""}'
diff --git a/browser_use/dom/enhanced_snapshot.py b/browser_use/dom/enhanced_snapshot.py
index 6fe0e814a..f9433145c 100644
--- a/browser_use/dom/enhanced_snapshot.py
+++ b/browser_use/dom/enhanced_snapshot.py
@@ -49,14 +49,18 @@ def build_snapshot_lookup(
device_pixel_ratio: float = 1.0,
) -> dict[int, EnhancedSnapshotNode]:
"""Build a lookup table of backend node ID to enhanced snapshot data with everything calculated upfront."""
+ import logging
+
+ logger = logging.getLogger('browser_use.dom.enhanced_snapshot')
snapshot_lookup: dict[int, EnhancedSnapshotNode] = {}
if not snapshot['documents']:
return snapshot_lookup
strings = snapshot['strings']
+ logger.debug(f'πŸ” SNAPSHOT: Processing {len(snapshot["documents"])} documents with {len(strings)} strings')
- for document in snapshot['documents']:
+ for doc_idx, document in enumerate(snapshot['documents']):
nodes: NodeTreeSnapshot = document['nodes']
layout: LayoutTreeSnapshot = document['layout']
@@ -66,6 +70,13 @@ def build_snapshot_lookup(
for i, backend_node_id in enumerate(nodes['backendNodeId']):
backend_node_to_snapshot_index[backend_node_id] = i
+ # Log document info
+ doc_url = strings[document.get('documentURL', 0)] if document.get('documentURL', 0) < len(strings) else 'N/A'
+ logger.debug(
+ f'πŸ” SNAPSHOT doc[{doc_idx}]: url={doc_url[:80]}... has {len(backend_node_to_snapshot_index)} nodes, '
+ f'layout has {len(layout.get("nodeIndex", []))} entries'
+ )
+
# PERFORMANCE: Pre-build layout index map to eliminate O(nΒ²) double lookups
# Preserve original behavior: use FIRST occurrence for duplicates
layout_index_map = {}
@@ -158,4 +169,7 @@ def build_snapshot_lookup(
stacking_contexts=stacking_contexts,
)
+ # Count how many have bounds (are actually visible/laid out)
+ with_bounds = sum(1 for n in snapshot_lookup.values() if n.bounds)
+ logger.debug(f'πŸ” SNAPSHOT: Built lookup with {len(snapshot_lookup)} total entries, {with_bounds} have bounds')
return snapshot_lookup
diff --git a/browser_use/dom/serializer/serializer.py b/browser_use/dom/serializer/serializer.py
index 2659469e6..35044e10d 100644
--- a/browser_use/dom/serializer/serializer.py
+++ b/browser_use/dom/serializer/serializer.py
@@ -600,6 +600,20 @@ class DOMTreeSerializer:
return False
+ def _is_inside_shadow_dom(self, node: SimplifiedNode) -> bool:
+ """Check if a node is inside a shadow DOM by walking up the parent chain.
+
+ Shadow DOM elements are descendants of a #document-fragment node (shadow root).
+ The shadow root node has node_type == DOCUMENT_FRAGMENT_NODE and shadow_root_type set.
+ """
+ current = node.original_node.parent_node
+ while current is not None:
+ # Shadow roots are DOCUMENT_FRAGMENT nodes with shadow_root_type
+ if current.node_type == NodeType.DOCUMENT_FRAGMENT_NODE and current.shadow_root_type is not None:
+ return True
+ current = current.parent_node
+ return False
+
def _assign_interactive_indices_and_mark_new_nodes(self, node: SimplifiedNode | None) -> None:
"""Assign interactive indices to clickable elements that are also visible."""
if not node:
@@ -612,6 +626,29 @@ class DOMTreeSerializer:
is_visible = node.original_node.snapshot_node and node.original_node.is_visible
is_scrollable = node.original_node.is_actually_scrollable
+ # DIAGNOSTIC: Log when interactive elements don't have snapshot_node
+ if is_interactive_assign and not node.original_node.snapshot_node:
+ import logging
+
+ logger = logging.getLogger('browser_use.dom.serializer')
+ attrs = node.original_node.attributes or {}
+ attr_str = f'name={attrs.get("name", "")} id={attrs.get("id", "")} type={attrs.get("type", "")}'
+ in_shadow = self._is_inside_shadow_dom(node)
+ if (
+ in_shadow
+ and node.original_node.tag_name
+ and node.original_node.tag_name.lower() in ['input', 'button', 'select', 'textarea', 'a']
+ ):
+ logger.debug(
+ f'πŸ” INCLUDING shadow DOM <{node.original_node.tag_name}> (no snapshot_node but in shadow DOM): '
+ f'backendNodeId={node.original_node.backend_node_id} {attr_str}'
+ )
+ else:
+ logger.debug(
+ f'πŸ” SKIPPING interactive <{node.original_node.tag_name}> (no snapshot_node, not in shadow DOM): '
+ f'backendNodeId={node.original_node.backend_node_id} {attr_str}'
+ )
+
# EXCEPTION: File inputs are often hidden with opacity:0 but are still functional
# Bootstrap and other frameworks use this pattern with custom-styled file pickers
is_file_input = (
@@ -621,6 +658,17 @@ class DOMTreeSerializer:
and node.original_node.attributes.get('type') == 'file'
)
+ # EXCEPTION: Shadow DOM form elements may not have snapshot layout data from CDP's
+ # DOMSnapshot.captureSnapshot, but they're still functional/interactive.
+ # This handles login forms, custom web components, etc. inside shadow DOM.
+ is_shadow_dom_element = (
+ is_interactive_assign
+ and not node.original_node.snapshot_node
+ and node.original_node.tag_name
+ and node.original_node.tag_name.lower() in ['input', 'button', 'select', 'textarea', 'a']
+ and self._is_inside_shadow_dom(node)
+ )
+
# Check if scrollable container should be made interactive
# For scrollable elements, ONLY make them interactive if they have no interactive descendants
should_make_interactive = False
@@ -631,8 +679,8 @@ class DOMTreeSerializer:
# Only make scrollable container interactive if it has NO interactive descendants
if not has_interactive_desc:
should_make_interactive = True
- elif is_interactive_assign and (is_visible or is_file_input):
- # Non-scrollable interactive elements: make interactive if visible (or file input)
+ elif is_interactive_assign and (is_visible or is_file_input or is_shadow_dom_element):
+ # Non-scrollable interactive elements: make interactive if visible (or file input or shadow DOM form element)
should_make_interactive = True
# Add to selector map if element should be interactive
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index d472400d4..427231a08 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -525,6 +525,25 @@ class DomService:
# Get snapshot data and calculate absolute position
snapshot_data = snapshot_lookup.get(node['backendNodeId'], None)
+
+ # DIAGNOSTIC: Log when interactive elements don't have snapshot data
+ if not snapshot_data and node['nodeName'].upper() in ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA', 'A']:
+ parent_has_shadow = False
+ parent_info = ''
+ if 'parentId' in node and node['parentId'] in enhanced_dom_tree_node_lookup:
+ parent = enhanced_dom_tree_node_lookup[node['parentId']]
+ if parent.shadow_root_type:
+ parent_has_shadow = True
+ parent_info = f'parent={parent.tag_name}(shadow={parent.shadow_root_type})'
+ attr_str = ''
+ if 'attributes' in node and node['attributes']:
+ attrs_dict = {node['attributes'][i]: node['attributes'][i + 1] for i in range(0, len(node['attributes']), 2)}
+ attr_str = f'name={attrs_dict.get("name", "N/A")} id={attrs_dict.get("id", "N/A")}'
+ self.logger.debug(
+ f'πŸ” NO SNAPSHOT DATA for <{node["nodeName"]}> backendNodeId={node["backendNodeId"]} '
+ f'{attr_str} {parent_info} (snapshot_lookup has {len(snapshot_lookup)} entries)'
+ )
+
absolute_position = None
if snapshot_data and snapshot_data.bounds:
absolute_position = DOMRect(