Skip to content

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

fixed parsing issue with non table data

Saurav Panda0b0adfb36cb6
diff --git a/browser_use/dom/markdown_extractor.py b/browser_use/dom/markdown_extractor.py
index a2d244721..cd86ccaec 100644
--- a/browser_use/dom/markdown_extractor.py
+++ b/browser_use/dom/markdown_extractor.py
@@ -474,8 +474,9 @@ def chunk_markdown_by_structure(
overlap = '\n'.join(prev_lines[-(overlap_lines):])
# Track table header from this chunk for next iteration.
- # Scan all blocks: the header+separator block could be anywhere in the chunk.
- prev_chunk_last_table_header = None
+ # Only overwrite if this chunk contains a new header+separator block;
+ # otherwise preserve the previous header so tables spanning 3+ chunks
+ # still get the header carried forward.
for b in chunk_blocks:
if b.block_type == _BlockType.TABLE:
hdr = _get_table_header(b)
diff --git a/browser_use/dom/serializer/html_serializer.py b/browser_use/dom/serializer/html_serializer.py
index dd3d1c6b6..01096abaf 100644
--- a/browser_use/dom/serializer/html_serializer.py
+++ b/browser_use/dom/serializer/html_serializer.py
@@ -121,6 +121,12 @@ class HTMLSerializer:
# Handle table normalization (ensure thead/tbody for markdownify)
if tag_name == 'table':
+ # Serialize shadow roots first (same as the general path)
+ if node.shadow_roots:
+ for shadow_root in node.shadow_roots:
+ child_html = self.serialize(shadow_root, depth + 1)
+ if child_html:
+ parts.append(child_html)
table_html = self._serialize_table_children(node, depth)
parts.append(table_html)
# Handle iframe content document
diff --git a/tests/ci/test_markdown_chunking.py b/tests/ci/test_markdown_chunking.py
index d47184419..68b178087 100644
--- a/tests/ci/test_markdown_chunking.py
+++ b/tests/ci/test_markdown_chunking.py
@@ -123,6 +123,27 @@ class TestChunkMarkdownTable:
assert '| Col1 | Col2 |' in chunks[1].overlap_prefix
assert '| --- | --- |' in chunks[1].overlap_prefix
+ def test_table_header_carried_across_three_plus_chunks(self):
+ """Table header must persist in overlap for ALL continuation chunks, not just the second."""
+ header = '| Col1 | Col2 |'
+ separator = '| --- | --- |'
+ rows = [f'| row{i} | data{i} |' for i in range(200)]
+ table = '\n'.join([header, separator] + rows)
+ content = table
+
+ # Force many small chunks
+ chunks = chunk_markdown_by_structure(content, max_chunk_chars=200)
+ assert len(chunks) >= 3, f'Expected >=3 chunks, got {len(chunks)}'
+
+ # Every chunk after the first should carry the table header in its overlap
+ for i in range(1, len(chunks)):
+ assert '| Col1 | Col2 |' in chunks[i].overlap_prefix, (
+ f'Chunk {i} missing table header in overlap'
+ )
+ assert '| --- | --- |' in chunks[i].overlap_prefix, (
+ f'Chunk {i} missing table separator in overlap'
+ )
+
class TestChunkMarkdownListItems:
"""List item continuations stay together."""