Skip to content

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

fix(dom): make image traversal allocation bounded

UniversePeak1cddc7a79416
diff --git a/browser_use/dom/serializer/serializer.py b/browser_use/dom/serializer/serializer.py
index 827a9a35e..8b86a9c75 100644
--- a/browser_use/dom/serializer/serializer.py
+++ b/browser_use/dom/serializer/serializer.py
@@ -933,14 +933,18 @@ class DOMTreeSerializer:
path_without_query = clean_src.split('?', 1)[0].split('#', 1)[0].rstrip('/')
return path_without_query.rsplit('/', 1)[-1]
- pending = list(reversed(node.children))
+ child_iterators = [iter(node.children)]
visited_descendants = 0
while (
- pending
+ child_iterators
and visited_descendants < DOMTreeSerializer.MAX_CHILD_IMAGE_DESCENDANTS
and len(image_context) < DOMTreeSerializer.MAX_CHILD_IMAGE_CONTEXTS
):
- current = pending.pop()
+ try:
+ current = next(child_iterators[-1])
+ except StopIteration:
+ child_iterators.pop()
+ continue
visited_descendants += 1
original_node = current.original_node
if original_node.node_type == NodeType.ELEMENT_NODE and original_node.tag_name == 'img':
@@ -963,7 +967,8 @@ class DOMTreeSerializer:
if parts:
image_context.append(' '.join(parts))
- pending.extend(reversed(current.children))
+ if current.children:
+ child_iterators.append(iter(current.children))
return ' '.join(image_context)
diff --git a/tests/ci/test_image_only_dom_representation.py b/tests/ci/test_image_only_dom_representation.py
index 65a6c92f5..1ff1d3fe9 100644
--- a/tests/ci/test_image_only_dom_representation.py
+++ b/tests/ci/test_image_only_dom_representation.py
@@ -86,6 +86,29 @@ def _simplified_image(backend_node_id: int, attributes: dict[str, str]) -> Simpl
return SimplifiedNode(original_node=image, children=[])
+class _NoEagerReverseList(list[SimplifiedNode]):
+ def __reversed__(self):
+ raise AssertionError('child lists must be traversed lazily')
+
+
+def test_child_image_context_finds_images_below_non_interactive_wrappers():
+ parent = _make_element_node(251, 'a', {'href': '/cards'}, x=10, y=10)
+ wrapper = _make_element_node(252, 'div', {}, x=12, y=12)
+ node = SimplifiedNode(
+ original_node=parent,
+ children=[
+ SimplifiedNode(
+ original_node=wrapper,
+ children=[_simplified_image(253, {'src': '/nested-card.png'})],
+ ),
+ ],
+ )
+
+ context = DOMTreeSerializer._get_child_image_context(node)
+
+ assert context == 'image_src=nested-card.png'
+
+
def test_child_image_context_strips_query_and_fragment_without_leaking_query_only_sources():
parent = _make_element_node(301, 'a', {'href': '/cards'}, x=10, y=10)
node = SimplifiedNode(
@@ -156,3 +179,15 @@ def test_child_image_context_caps_traversal_even_when_images_have_no_context():
context = DOMTreeSerializer._get_child_image_context(node)
assert context == ''
+
+
+def test_child_image_context_does_not_copy_wide_child_lists_before_traversal():
+ parent = _make_element_node(701, 'a', {'href': '/gallery'}, x=10, y=10)
+ node = SimplifiedNode(original_node=parent, children=[])
+ node.children = _NoEagerReverseList(
+ _simplified_image(702 + index, {'src': 'data:image/png;base64,ignored'}) for index in range(200)
+ )
+
+ context = DOMTreeSerializer._get_child_image_context(node)
+
+ assert context == ''