Skip to content

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

First version

Magnus Müller6316ae693bb3
diff --git a/browser_use/dom/serializer/html_serializer.py b/browser_use/dom/serializer/html_serializer.py
new file mode 100644
index 000000000..3715bc72f
--- /dev/null
+++ b/browser_use/dom/serializer/html_serializer.py
@@ -0,0 +1,184 @@
+# @file purpose: Serializes enhanced DOM trees to HTML format including shadow roots
+
+from browser_use.dom.views import EnhancedDOMTreeNode, NodeType
+
+
+class HTMLSerializer:
+ """Serializes enhanced DOM trees back to HTML format.
+
+ This serializer reconstructs HTML from the enhanced DOM tree, including:
+ - Shadow DOM content (both open and closed)
+ - Iframe content documents
+ - All attributes and text nodes
+ - Proper HTML structure
+
+ Unlike getOuterHTML which only captures light DOM, this captures the full
+ enhanced tree including shadow roots that are crucial for modern SPAs.
+ """
+
+ def __init__(self, extract_links: bool = False):
+ """Initialize the HTML serializer.
+
+ Args:
+ extract_links: If True, preserves all links. If False, removes href attributes.
+ """
+ self.extract_links = extract_links
+
+ def serialize(self, node: EnhancedDOMTreeNode, depth: int = 0) -> str:
+ """Serialize an enhanced DOM tree node to HTML.
+
+ Args:
+ node: The enhanced DOM tree node to serialize
+ depth: Current depth for indentation (internal use)
+
+ Returns:
+ HTML string representation of the node and its descendants
+ """
+ if node.node_type == NodeType.DOCUMENT_NODE:
+ # Process document root - serialize all children
+ parts = []
+ for child in node.children_and_shadow_roots:
+ child_html = self.serialize(child, depth)
+ if child_html:
+ parts.append(child_html)
+ return ''.join(parts)
+
+ elif node.node_type == NodeType.DOCUMENT_FRAGMENT_NODE:
+ # Shadow DOM root - wrap in template with shadowrootmode attribute
+ # This is the declarative shadow DOM syntax that html2text can process
+ parts = []
+
+ # Add shadow root opening
+ shadow_type = node.shadow_root_type or 'open'
+ parts.append(f'<template shadowrootmode="{shadow_type.lower()}">')
+
+ # Serialize shadow children
+ for child in node.children:
+ child_html = self.serialize(child, depth + 1)
+ if child_html:
+ parts.append(child_html)
+
+ # Close shadow root
+ parts.append('</template>')
+
+ return ''.join(parts)
+
+ elif node.node_type == NodeType.ELEMENT_NODE:
+ parts = []
+ tag_name = node.tag_name.lower()
+
+ # Skip non-content elements
+ if tag_name in {'style', 'script', 'head', 'meta', 'link', 'title'}:
+ return ''
+
+ # Opening tag
+ parts.append(f'<{tag_name}')
+
+ # Add attributes
+ if node.attributes:
+ attrs = self._serialize_attributes(node.attributes)
+ if attrs:
+ parts.append(' ' + attrs)
+
+ # Handle void elements (self-closing)
+ void_elements = {
+ 'area',
+ 'base',
+ 'br',
+ 'col',
+ 'embed',
+ 'hr',
+ 'img',
+ 'input',
+ 'link',
+ 'meta',
+ 'param',
+ 'source',
+ 'track',
+ 'wbr',
+ }
+ if tag_name in void_elements:
+ parts.append(' />')
+ return ''.join(parts)
+
+ parts.append('>')
+
+ # Handle iframe content document
+ if tag_name in {'iframe', 'frame'} and node.content_document:
+ # Serialize iframe content
+ for child in node.content_document.children_nodes or []:
+ child_html = self.serialize(child, depth + 1)
+ if child_html:
+ parts.append(child_html)
+ else:
+ # Process children and shadow roots
+ for child in node.children_and_shadow_roots:
+ child_html = self.serialize(child, depth + 1)
+ if child_html:
+ parts.append(child_html)
+
+ # Closing tag
+ parts.append(f'</{tag_name}>')
+
+ return ''.join(parts)
+
+ elif node.node_type == NodeType.TEXT_NODE:
+ # Return text content with basic HTML escaping
+ if node.node_value:
+ return self._escape_html(node.node_value)
+ return ''
+
+ elif node.node_type == NodeType.COMMENT_NODE:
+ # Skip comments to reduce noise
+ return ''
+
+ else:
+ # Unknown node type - skip
+ return ''
+
+ def _serialize_attributes(self, attributes: dict[str, str]) -> str:
+ """Serialize element attributes to HTML attribute string.
+
+ Args:
+ attributes: Dictionary of attribute names to values
+
+ Returns:
+ HTML attribute string (e.g., 'class="foo" id="bar"')
+ """
+ parts = []
+ for key, value in attributes.items():
+ # Skip href if not extracting links
+ if not self.extract_links and key == 'href':
+ continue
+
+ # Handle boolean attributes
+ if value == '' or value is None:
+ parts.append(key)
+ else:
+ # Escape attribute value
+ escaped_value = self._escape_attribute(value)
+ parts.append(f'{key}="{escaped_value}"')
+
+ return ' '.join(parts)
+
+ def _escape_html(self, text: str) -> str:
+ """Escape HTML special characters in text content.
+
+ Args:
+ text: Raw text content
+
+ Returns:
+ HTML-escaped text
+ """
+ return text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
+
+ def _escape_attribute(self, value: str) -> str:
+ """Escape HTML special characters in attribute values.
+
+ Args:
+ value: Raw attribute value
+
+ Returns:
+ HTML-escaped attribute value
+ """
+ return value.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#x27;')
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index ef90026ad..fbde50749 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -1047,17 +1047,24 @@ You will be given a query and the markdown of a webpage that has been filtered t
"""
import re
- # Get HTML content from current page
- cdp_session = await browser_session.get_or_create_cdp_session()
- try:
- body_id = await cdp_session.cdp_client.send.DOM.getDocument(session_id=cdp_session.session_id)
- page_html_result = await cdp_session.cdp_client.send.DOM.getOuterHTML(
- params={'backendNodeId': body_id['root']['backendNodeId']}, session_id=cdp_session.session_id
- )
- page_html = page_html_result['outerHTML']
- current_url = await browser_session.get_current_page_url()
- except Exception as e:
- raise RuntimeError(f"Couldn't extract page content: {e}")
+ current_url = await browser_session.get_current_page_url()
+
+ # Get the enhanced DOM tree from DOMWatchdog
+ # This captures the current state of the page including dynamic content, shadow roots, etc.
+ dom_watchdog = browser_session._dom_watchdog
+ assert dom_watchdog is not None, 'DOMWatchdog not available'
+
+ # Get or build the enhanced DOM tree
+ await dom_watchdog._build_dom_tree_without_highlights()
+ enhanced_dom_tree = dom_watchdog.enhanced_dom_tree
+ assert enhanced_dom_tree is not None, 'Enhanced DOM tree not available'
+
+ # Use the new HTML serializer with the enhanced DOM tree
+ # This serializer captures shadow roots and iframe content that getOuterHTML misses
+ from browser_use.dom.serializer.html_serializer import HTMLSerializer
+
+ html_serializer = HTMLSerializer(extract_links=extract_links)
+ page_html = html_serializer.serialize(enhanced_dom_tree)
original_html_length = len(page_html)
@@ -1086,6 +1093,7 @@ You will be given a query and the markdown of a webpage that has been filtered t
# Content statistics
stats = {
'url': current_url,
+ 'method': 'enhanced_dom_tree', # Mark this as using the enhanced DOM tree method
'original_html_chars': original_html_length,
'initial_markdown_chars': initial_markdown_length,
'filtered_chars_removed': chars_filtered,