Skip to content

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

Show the live value of pre-filled inputs to the agent

Magnus Müller5ffd3090adf2
diff --git a/browser_use/dom/enhanced_snapshot.py b/browser_use/dom/enhanced_snapshot.py
index eb13ff51c..0e838dda9 100644
--- a/browser_use/dom/enhanced_snapshot.py
+++ b/browser_use/dom/enhanced_snapshot.py
@@ -91,6 +91,18 @@ def build_snapshot_lookup(
has_clickable_data = 'isClickable' in nodes
is_clickable_set: set[int] = set(nodes['isClickable']['index']) if has_clickable_data else set()
+ # Live form values live in the snapshot, not in the DOM attributes. Map
+ # snapshot index -> string once so each node lookup stays O(1).
+ input_value_by_index: dict[int, str] = {}
+ for key in ('inputValue', 'textValue'):
+ rare = nodes.get(key)
+ if rare:
+ for idx, string_index in zip(rare.get('index', []), rare.get('value', [])):
+ if 0 <= string_index < len(strings):
+ input_value_by_index[idx] = strings[string_index]
+ input_checked_set: set[int] = set(nodes['inputChecked']['index']) if 'inputChecked' in nodes else set()
+ has_checked_data = 'inputChecked' in nodes
+
# Build snapshot lookup for each backend node id
for backend_node_id, snapshot_index in backend_node_to_snapshot_index.items():
is_clickable = None
@@ -173,6 +185,8 @@ def build_snapshot_lookup(
computed_styles=computed_styles if computed_styles else None,
paint_order=paint_order,
stacking_contexts=stacking_contexts,
+ input_value=input_value_by_index.get(snapshot_index),
+ input_checked=(snapshot_index in input_checked_set) if has_checked_data else None,
)
# Count how many have bounds (are actually visible/laid out)
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index 16e04cf63..03e5c55c4 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -811,6 +811,15 @@ class DomService:
# Get snapshot data and calculate absolute position
snapshot_data = snapshot_lookup.get(node['backendNodeId'], None)
+ # Show the value a field currently holds, not only the static attribute.
+ # JS, autofill, and framework bindings set the property without touching
+ # the attribute, so the agent used to see pre-filled fields as empty (#5647).
+ if snapshot_data and snapshot_data.input_value is not None and node['nodeName'].upper() in ('INPUT', 'TEXTAREA'):
+ input_type = (attributes or {}).get('type', '').lower()
+ if input_type not in ('password', 'file', 'hidden'):
+ attributes = dict(attributes or {})
+ attributes['value'] = snapshot_data.input_value
+
# DIAGNOSTIC: Log when interactive elements don't have snapshot data
if not snapshot_data and node['nodeName'].upper() in ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA', 'A']:
parent_has_shadow = False
diff --git a/browser_use/dom/views.py b/browser_use/dom/views.py
index 3c6493ff4..86b773bb3 100644
--- a/browser_use/dom/views.py
+++ b/browser_use/dom/views.py
@@ -353,6 +353,10 @@ class EnhancedSnapshotNode:
"""Paint order from the layout tree"""
stacking_contexts: int | None
"""Stacking contexts from the layout tree"""
+ input_value: str | None = None
+ """Live value of an <input> or <textarea> (DOMSnapshot inputValue/textValue), which the value attribute misses when JS, autofill, or a framework set it."""
+ input_checked: bool | None = None
+ """Live checked state of a checkbox or radio input (DOMSnapshot inputChecked)."""
# @dataclass(slots=True)
diff --git a/tests/ci/test_dom_live_input_value.py b/tests/ci/test_dom_live_input_value.py
new file mode 100644
index 000000000..5517ef880
--- /dev/null
+++ b/tests/ci/test_dom_live_input_value.py
@@ -0,0 +1,53 @@
+"""The DOM the agent sees must show the value a form field currently holds.
+
+Regression test for #5647: values set by JavaScript, autofill, or a framework
+live in the element's `value` property, not in the `value` attribute, so the
+agent used to see pre-filled fields as empty.
+"""
+
+import pytest
+from pytest_httpserver import HTTPServer
+
+from browser_use.browser.events import NavigateToUrlEvent
+
+PAGE = """<!DOCTYPE html>
+<html><head><title>Prefilled form</title></head>
+<body>
+ <label for="name">Name</label>
+ <input id="name" type="text" placeholder="Your name">
+ <label for="notes">Notes</label>
+ <textarea id="notes"></textarea>
+ <input id="secret" type="password">
+ <script>
+ document.getElementById('name').value = 'Ada Lovelace';
+ document.getElementById('notes').value = 'call back tuesday';
+ document.getElementById('secret').value = 'hunter2';
+ </script>
+</body></html>"""
+
+
[email protected](scope='module')
+def http_server():
+ server = HTTPServer()
+ server.start()
+ server.expect_request('/prefilled').respond_with_data(PAGE, content_type='text/html')
+ yield server
+ server.stop()
+
+
+async def test_live_input_values_reach_the_agent(browser_session, http_server):
+ event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=http_server.url_for('/prefilled')))
+ await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+
+ state = await browser_session.get_browser_state_summary()
+ by_id = {node.attributes.get('id'): node for node in state.dom_state.selector_map.values() if node.attributes}
+
+ assert by_id['name'].attributes.get('value') == 'Ada Lovelace'
+ assert by_id['notes'].attributes.get('value') == 'call back tuesday'
+ assert by_id['secret'].attributes.get('value') is None, 'password values must not be exposed'
+
+ llm_view = state.dom_state.llm_representation()
+ assert 'Ada Lovelace' in llm_view
+ assert 'call back tuesday' in llm_view
+ assert 'hunter2' not in llm_view