Skip to content

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

fixed duplicate text bug

Gregor Žuničc5faf50bfb63
diff --git a/browser_use/dom/playground/extraction.py b/browser_use/dom/playground/extraction.py
index aa2028589..a502779a3 100644
--- a/browser_use/dom/playground/extraction.py
+++ b/browser_use/dom/playground/extraction.py
@@ -4,6 +4,7 @@ import os
import time
import anyio
+import pyperclip
import tiktoken
from browser_use.agent.prompts import AgentMessagePrompt
@@ -94,7 +95,7 @@ async def test_focus_vs_all_elements():
# save pure clickable elements to a file
async with await anyio.open_file('./tmp/element_tree.json', 'w', encoding='utf-8') as f:
- await f.write(json.dumps(all_elements_state.element_tree.__json__(), indent=2))
+ await f.write(json.dumps(all_elements_state.element_tree.__json__(), indent=0))
# copy the user message to the clipboard
# pyperclip.copy(text_to_save)
@@ -111,13 +112,32 @@ async def test_focus_vs_all_elements():
# f.write(json.dumps(all_elements_state.element_tree.__json__(), indent=2))
# print('Clickable elements written to ./tmp/clickable_elements.json')
- answer = input("Enter element index to click, 'index,text' to input, or 'q' to quit: ")
+ answer = input(
+ "Enter element index to click, 'index,text' to input, 'c,index' to copy element JSON, or 'q' to quit: "
+ )
if answer.lower() == 'q':
break
try:
- if ',' in answer:
+ if answer.lower().startswith('c,'):
+ # Copy element JSON format: c,index
+ parts = answer.split(',', 1)
+ if len(parts) == 2:
+ try:
+ target_index = int(parts[1].strip())
+ if target_index in selector_map:
+ element_node = selector_map[target_index]
+ element_json = json.dumps(element_node.__json__(), indent=2, default=str)
+ pyperclip.copy(element_json)
+ print(f'Copied element {target_index} JSON to clipboard: {element_node.tag_name}')
+ else:
+ print(f'Invalid index: {target_index}')
+ except ValueError:
+ print(f'Invalid index format: {parts[1]}')
+ else:
+ print("Invalid input format. Use 'c,index'.")
+ elif ',' in answer:
# Input text format: index,text
parts = answer.split(',', 1)
if len(parts) == 2:
@@ -149,7 +169,7 @@ async def test_focus_vs_all_elements():
else:
print(f'Invalid index: {clicked_index}')
except ValueError:
- print(f"Invalid input: '{answer}'. Enter an index, 'index,text', or 'q'.")
+ print(f"Invalid input: '{answer}'. Enter an index, 'index,text', 'c,index', or 'q'.")
except Exception as action_e:
print(f'Action failed: {action_e}')
diff --git a/browser_use/dom/views.py b/browser_use/dom/views.py
index 7b83bfcff..5d4acabba 100644
--- a/browser_use/dom/views.py
+++ b/browser_use/dom/views.py
@@ -259,6 +259,7 @@ class DOMElementNode(DOMBaseNode):
if not attributes_html_str:
line += ' '
line += f'>{text}'
+
# Add space before /> only if neither attributes NOR text were added
elif not attributes_html_str:
line += ' '
@@ -272,9 +273,10 @@ class DOMElementNode(DOMBaseNode):
elif isinstance(node, DOMTextNode):
# Add text only if it doesn't have a highlighted parent
- if (
- node.parent and node.parent.highlight_index is None and node.parent.is_visible and node.parent.is_top_element
- ): # and node.is_parent_top_element()
+ if node.has_parent_with_highlight_index():
+ return
+
+ if node.parent and node.parent.is_visible and node.parent.is_top_element:
formatted_text.append(f'{depth_str}{node.text}')
process_node(self, 0)