Skip to content

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

Only include unique attribute values in state - no attribute keys

Magnus Müllerf6770dee40c5
diff --git a/browser_use/dom/views.py b/browser_use/dom/views.py
index 30d59716a..830ce32df 100644
--- a/browser_use/dom/views.py
+++ b/browser_use/dom/views.py
@@ -128,12 +128,17 @@ class DOMElementNode(DOMBaseNode):
if node.highlight_index is not None:
attributes_str = ''
if include_attributes:
- attributes_str = ' ' + ' '.join(
- f'{key}="{value}"' for key, value in node.attributes.items() if key in include_attributes
+ attributes = list(
+ set([str(value) for key, value in node.attributes.items() if key in include_attributes])
)
- formatted_text.append(
- f'[{node.highlight_index}]<{node.tag_name}{attributes_str}>{node.get_all_text_till_next_clickable_element()}</{node.tag_name}>'
- )
+ attributes_str = ';'.join(attributes)
+ text = node.get_all_text_till_next_clickable_element()
+ if text:
+ formatted_text.append(
+ f'[{node.highlight_index}]<{node.tag_name} {attributes_str}>{text}</{node.tag_name}>'
+ )
+ else:
+ formatted_text.append(f'[{node.highlight_index}]<{node.tag_name} {attributes_str}/>')
# Process children regardless
for child in node.children: