Skip to content

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

fix(dom): normalize hidden code styles

Yahoo17cbc07ecdaa
diff --git a/browser_use/dom/serializer/html_serializer.py b/browser_use/dom/serializer/html_serializer.py
index 097ca2192..fcc1d8499 100644
--- a/browser_use/dom/serializer/html_serializer.py
+++ b/browser_use/dom/serializer/html_serializer.py
@@ -74,7 +74,8 @@ class HTMLSerializer:
if tag_name == 'code' and node.attributes:
style = node.attributes.get('style', '')
# Check if element is hidden (display:none) - likely JSON data
- if 'display:none' in style.replace(' ', '') or 'display: none' in style:
+ normalized_style = ''.join(style.lower().split())
+ if 'display:none' in normalized_style:
return ''
# Also check for bpr-guid IDs (LinkedIn's JSON data pattern)
element_id = node.attributes.get('id', '')
diff --git a/tests/ci/test_html_serializer_style.py b/tests/ci/test_html_serializer_style.py
new file mode 100644
index 000000000..4e3b74149
--- /dev/null
+++ b/tests/ci/test_html_serializer_style.py
@@ -0,0 +1,28 @@
+from urllib.parse import quote
+
+import pytest
+
+from browser_use.dom.markdown_extractor import extract_clean_markdown
+
+
+ 'style',
+ [
+ 'display: none',
+ 'Display: None',
+ 'DISPLAY:\tNONE',
+ 'color: red; display : none !important',
+ ],
+)
+async def test_hidden_code_inline_style_is_case_insensitive(browser_session, style: str):
+ html = f'<main>visible control</main><code id="snippet" style="{style}">hidden state payload</code>'
+ await browser_session.navigate_to('data:text/html,' + quote(html))
+
+ page = await browser_session.get_current_page()
+ assert page is not None
+ computed_display = await page.evaluate("() => getComputedStyle(document.querySelector('#snippet')).display")
+ markdown, _ = await extract_clean_markdown(browser_session=browser_session)
+
+ assert computed_display == 'none'
+ assert 'visible control' in markdown
+ assert 'hidden state payload' not in markdown