Skip to content

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

Fix: newlines in attribute values

Edward Suncd938016bbf6
diff --git a/browser_use/browser/context.py b/browser_use/browser/context.py
index 06c4bfa45..523d610a1 100644
--- a/browser_use/browser/context.py
+++ b/browser_use/browser/context.py
@@ -1212,6 +1212,9 @@ class BrowserContext:
css_selector += f'[{safe_attribute}]'
elif any(char in value for char in '"\'<>`\n\r\t'):
# Use contains for values with special characters
+ # For newline-containing text, only use the part before the newline
+ if '\n' in value:
+ value = value.split('\n')[0]
# Regex-substitute *any* whitespace with a single space, then strip.
collapsed_value = re.sub(r'\s+', ' ', value).strip()
# Escape embedded double-quotes.