Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
improve text field handling
Saurav Panda749fa56d1af8
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.pyindex 7a19966f3..05217658f 100644--- a/browser_use/browser/watchdogs/default_action_watchdog.py+++ b/browser_use/browser/watchdogs/default_action_watchdog.py@@ -1885,6 +1885,54 @@ class DefaultActionWatchdog(BaseWatchdog):except Exception as e:self.logger.debug(f'Value readback failed (non-critical): {e}')+ # Step 6: Auto-retry on concatenation mismatch (only when clear was requested)+ # If we asked to clear but the readback value contains the typed text as a substring+ # yet is longer, the field had pre-existing text that wasn't cleared. Set directly.+ if clear and not is_sensitive and input_coordinates and 'actual_value' in input_coordinates:+ actual_value = input_coordinates['actual_value']+ if actual_value is not None and actual_value != text and len(actual_value) > len(text) and text in actual_value:+ self.logger.info(f'🔄 Concatenation detected: got "{actual_value}", expected "{text}" — auto-retrying')+ try:+ # Clear + set value via native setter in one JS call (works with React/Vue)+ retry_result = await cdp_session.cdp_client.send.Runtime.callFunctionOn(+ params={+ 'objectId': object_id,+ 'functionDeclaration': """+ function(newValue) {+ if (this.value !== undefined) {+ var desc = Object.getOwnPropertyDescriptor(+ HTMLInputElement.prototype, 'value'+ ) || Object.getOwnPropertyDescriptor(+ HTMLTextAreaElement.prototype, 'value'+ );+ if (desc && desc.set) {+ desc.set.call(this, newValue);+ } else {+ this.value = newValue;+ }+ } else if (this.isContentEditable) {+ this.textContent = newValue;+ }+ this.dispatchEvent(new Event('input', { bubbles: true }));+ this.dispatchEvent(new Event('change', { bubbles: true }));+ return this.value !== undefined ? this.value : this.textContent;+ }+ """,+ 'arguments': [{'value': text}],+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ retry_value = retry_result.get('result', {}).get('value')+ if retry_value is not None:+ input_coordinates['actual_value'] = retry_value+ if retry_value == text:+ self.logger.info('✅ Auto-retry fixed concatenation')+ else:+ self.logger.warning(f'⚠️ Auto-retry value still differs: "{retry_value}"')+ except Exception as e:+ self.logger.debug(f'Auto-retry failed (non-critical): {e}')+# Return coordinates metadata if availablereturn input_coordinatesdiff --git a/browser_use/tools/service.py b/browser_use/tools/service.pyindex fda386293..e51839cb9 100644--- a/browser_use/tools/service.py+++ b/browser_use/tools/service.py@@ -664,9 +664,10 @@ class Tools(Generic[Context]):if not has_sensitive_data and actual_value is not None and actual_value != params.text:msg += f"\n⚠️ Note: the field's actual value '{actual_value}' differs from typed text '{params.text}'. The page may have reformatted or autocompleted your input."- # Check for autocomplete/combobox field+ # Check for autocomplete/combobox field — add mechanical delay for dropdownif _is_autocomplete_field(node):msg += '\n💡 This is an autocomplete field. Wait for suggestions to appear, then click the correct suggestion instead of pressing Enter.'+ await asyncio.sleep(0.4) # let dropdown populate before next action# Include input coordinates in metadata if availablereturn ActionResult(diff --git a/tests/ci/interactions/test_autocomplete_interaction.py b/tests/ci/interactions/test_autocomplete_interaction.pyindex 17b59bfdc..847ea056d 100644--- a/tests/ci/interactions/test_autocomplete_interaction.py+++ b/tests/ci/interactions/test_autocomplete_interaction.py@@ -1,4 +1,4 @@-"""Test autocomplete/combobox field detection and value readback.+"""Test autocomplete/combobox field detection, value readback, and input clearing.Tests cover:- Value mismatch detection when JS rewrites input value@@ -6,6 +6,10 @@ Tests cover:- Datalist field detection (input with list attribute)- No false positives on plain inputs- Sensitive data skips value verification+- Pre-filled input clearing (clear=True default)+- Pre-filled input appending (clear=False)+- Concatenation auto-retry when clear fails+- Autocomplete delay before next action"""import asyncio@@ -101,6 +105,47 @@ def http_server():content_type='text/html',)+ # Page 5: Pre-filled input to test clear=True behavior+ server.expect_request('/prefilled-input').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head><title>Pre-filled Input Test</title></head>+ <body>+ <input id="prefilled" type="text" value="old value" />+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Page 6: Input where clear fails — input event listener restores old text+ # Simulates a framework-controlled input where clearing triggers re-render with old state+ server.expect_request('/sticky-input').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head><title>Sticky Input Test</title></head>+ <body>+ <input id="sticky" type="text" value="prefix_" />+ <script>+ var el = document.getElementById('sticky');+ var clearAttempts = 0;+ // Intercept value clears: restore old value on first two clears+ // (simulates framework re-rendering with stale state)+ el.addEventListener('input', function() {+ if (el.value === '' && clearAttempts < 2) {+ clearAttempts++;+ el.value = 'prefix_';+ }+ });+ </script>+ </body>+ </html>+ """,+ content_type='text/html',+ )+yield serverserver.stop()@@ -226,3 +271,94 @@ class TestAutocompleteInteraction:assert 'differs from typed text' not in result.extracted_content, (f'Sensitive data should not show value mismatch: {result.extracted_content}')++ async def test_prefilled_input_cleared_by_default(self, tools: Tools, browser_session: BrowserSession, base_url: str):+ """Type into a pre-filled input with clear=True (default). Field should contain only the new text."""+ await tools.navigate(url=f'{base_url}/prefilled-input', new_tab=False, browser_session=browser_session)+ await asyncio.sleep(0.3)+ await browser_session.get_browser_state_summary()++ idx = await browser_session.get_index_by_id('prefilled')+ assert idx is not None, 'Could not find prefilled input'++ result = await tools.input(index=idx, text='new value', browser_session=browser_session)++ assert isinstance(result, ActionResult)+ assert result.error is None, f'Input action failed: {result.error}'++ # Read back the actual DOM value via CDP+ cdp_session = await browser_session.get_or_create_cdp_session()+ readback = await cdp_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('prefilled').value"},+ session_id=cdp_session.session_id,+ )+ actual = readback.get('result', {}).get('value', '')+ assert actual == 'new value', f'Expected "new value", got "{actual}" — clear=True did not remove old text'++ async def test_prefilled_input_append_with_clear_false(self, tools: Tools, browser_session: BrowserSession, base_url: str):+ """Type into a pre-filled input with clear=False. Field should contain old + new text."""+ await tools.navigate(url=f'{base_url}/prefilled-input', new_tab=False, browser_session=browser_session)+ await asyncio.sleep(0.3)+ await browser_session.get_browser_state_summary()++ idx = await browser_session.get_index_by_id('prefilled')+ assert idx is not None, 'Could not find prefilled input'++ result = await tools.input(index=idx, text=' appended', clear=False, browser_session=browser_session)++ assert isinstance(result, ActionResult)+ assert result.error is None, f'Input action failed: {result.error}'++ # Read back the actual DOM value via CDP+ cdp_session = await browser_session.get_or_create_cdp_session()+ readback = await cdp_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('prefilled').value"},+ session_id=cdp_session.session_id,+ )+ actual = readback.get('result', {}).get('value', '')+ assert 'old value' in actual and 'appended' in actual, f'Expected old text + appended text, got "{actual}"'++ async def test_concatenation_retry_on_sticky_field(self, tools: Tools, browser_session: BrowserSession, base_url: str):+ """Type into a field where clearing is resisted by JS. The retry should fix the value."""+ await tools.navigate(url=f'{base_url}/sticky-input', new_tab=False, browser_session=browser_session)+ await asyncio.sleep(0.3)+ await browser_session.get_browser_state_summary()++ idx = await browser_session.get_index_by_id('sticky')+ assert idx is not None, 'Could not find sticky input'++ result = await tools.input(index=idx, text='typed_text', browser_session=browser_session)++ assert isinstance(result, ActionResult)+ assert result.error is None, f'Input action failed: {result.error}'++ # The retry mechanism uses a native setter to bypass the event listener.+ # Read back the final DOM value.+ cdp_session = await browser_session.get_or_create_cdp_session()+ readback = await cdp_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('sticky').value"},+ session_id=cdp_session.session_id,+ )+ actual = readback.get('result', {}).get('value', '')+ # The retry should have set the value to just "typed_text" via the native setter.+ # Even if the event listener fires on the retry's dispatched events, the native setter+ # bypasses instance-level interception. The value may or may not be perfect depending+ # on how the JS listener interacts, but it should not be "prefix_typed_text" (raw concatenation).+ assert actual != 'prefix_typed_text', f'Got raw concatenation "{actual}" — retry should have prevented this'++ async def test_autocomplete_field_adds_delay(self, tools: Tools, browser_session: BrowserSession, base_url: str):+ """Typing into an autocomplete field should take >= 400ms due to the mechanical delay."""+ import time++ await tools.navigate(url=f'{base_url}/combobox-field', new_tab=False, browser_session=browser_session)+ await asyncio.sleep(0.3)+ await browser_session.get_browser_state_summary()+ combo_idx = await browser_session.get_index_by_id('combo')+ assert combo_idx is not None++ t0 = time.monotonic()+ await tools.input(index=combo_idx, text='hi', browser_session=browser_session)+ duration = time.monotonic() - t0++ # The 400ms sleep is a hard floor — total duration must exceed it+ assert duration >= 0.4, f'Autocomplete delay not present: input took only {duration:.3f}s (expected >= 0.4s)'