Skip to content

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

SVG fix

Magnus Müller6ee07c06ecb1
diff --git a/browser_use/dom/serializer/serializer.py b/browser_use/dom/serializer/serializer.py
index ae5bf4ef3..1c2cec6e9 100644
--- a/browser_use/dom/serializer/serializer.py
+++ b/browser_use/dom/serializer/serializer.py
@@ -17,6 +17,26 @@ from browser_use.dom.views import (
DISABLED_ELEMENTS = {'style', 'script', 'head', 'meta', 'link', 'title'}
+# SVG child elements to skip (decorative only, no interaction value)
+SVG_ELEMENTS = {
+ 'path',
+ 'rect',
+ 'g',
+ 'circle',
+ 'ellipse',
+ 'line',
+ 'polyline',
+ 'polygon',
+ 'use',
+ 'defs',
+ 'clipPath',
+ 'mask',
+ 'pattern',
+ 'image',
+ 'text',
+ 'tspan',
+}
+
class DOMTreeSerializer:
"""Serializes enhanced DOM trees to string format."""
@@ -450,6 +470,10 @@ class DOMTreeSerializer:
if node.node_name.lower() in DISABLED_ELEMENTS:
return None
+ # Skip SVG child elements entirely (path, rect, g, circle, etc.)
+ if node.node_name.lower() in SVG_ELEMENTS:
+ return None
+
if node.node_name == 'IFRAME' or node.node_name == 'FRAME':
if node.content_document:
simplified = SimplifiedNode(original_node=node, children=[])
@@ -754,6 +778,32 @@ class DOMTreeSerializer:
formatted_text.append(child_text)
return '\n'.join(formatted_text)
+ # Special handling for SVG elements - show the tag but collapse children
+ if node.original_node.tag_name.lower() == 'svg':
+ shadow_prefix = ''
+ if node.is_shadow_host:
+ has_closed_shadow = any(
+ child.original_node.node_type == NodeType.DOCUMENT_FRAGMENT_NODE
+ and child.original_node.shadow_root_type
+ and child.original_node.shadow_root_type.lower() == 'closed'
+ for child in node.children
+ )
+ shadow_prefix = '|SHADOW(closed)|' if has_closed_shadow else '|SHADOW(open)|'
+
+ line = f'{depth_str}{shadow_prefix}'
+ # Add interactive marker if clickable
+ if node.is_interactive:
+ new_prefix = '*' if node.is_new else ''
+ line += f'{new_prefix}[{node.original_node.backend_node_id}]'
+ line += '<svg'
+ attributes_html_str = DOMTreeSerializer._build_attributes_string(node.original_node, include_attributes, '')
+ if attributes_html_str:
+ line += f' {attributes_html_str}'
+ line += ' /> <!-- SVG content collapsed -->'
+ formatted_text.append(line)
+ # Don't process children for SVG
+ return '\n'.join(formatted_text)
+
# Add element if clickable, scrollable, or iframe
is_any_scrollable = node.original_node.is_actually_scrollable or node.original_node.is_scrollable
should_show_scroll = node.original_node.should_show_scroll_info