Skip to content

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

improved concatenation heuristic

Saurav Panda5d7ff041d35c
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.py
index 7a1ef034f..f314c458d 100644
--- a/browser_use/browser/watchdogs/default_action_watchdog.py
+++ b/browser_use/browser/watchdogs/default_action_watchdog.py
@@ -1894,7 +1894,7 @@ class DefaultActionWatchdog(BaseWatchdog):
isinstance(actual_value, str)
and actual_value != text
and len(actual_value) > len(text)
- and text in actual_value
+ and (actual_value.endswith(text) or actual_value.startswith(text))
):
self.logger.info(f'πŸ”„ Concatenation detected: got "{actual_value}", expected "{text}" β€” auto-retrying')
try:
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index e51839cb9..da2fa25b9 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -667,7 +667,11 @@ class Tools(Generic[Context]):
# Check for autocomplete/combobox field β€” add mechanical delay for dropdown
if _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
+ # Only delay for true JS-driven autocomplete (combobox / aria-autocomplete),
+ # not native <datalist> or loose aria-haspopup which the browser handles instantly
+ attrs = node.attributes or {}
+ if attrs.get('role') == 'combobox' or (attrs.get('aria-autocomplete', '') not in ('', 'none')):
+ await asyncio.sleep(0.4) # let JS dropdown populate before next action
# Include input coordinates in metadata if available
return ActionResult(
diff --git a/tests/ci/interactions/test_autocomplete_interaction.py b/tests/ci/interactions/test_autocomplete_interaction.py
index 847ea056d..ac8c97d45 100644
--- a/tests/ci/interactions/test_autocomplete_interaction.py
+++ b/tests/ci/interactions/test_autocomplete_interaction.py
@@ -346,8 +346,8 @@ class TestAutocompleteInteraction:
# 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."""
+ async def test_combobox_field_adds_delay(self, tools: Tools, browser_session: BrowserSession, base_url: str):
+ """Typing into a combobox (role=combobox) 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)
@@ -361,4 +361,22 @@ class TestAutocompleteInteraction:
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)'
+ assert duration >= 0.4, f'Combobox delay not present: input took only {duration:.3f}s (expected >= 0.4s)'
+
+ async def test_datalist_field_no_delay(self, tools: Tools, browser_session: BrowserSession, base_url: str):
+ """Native datalist fields should NOT get the 400ms delay β€” browser handles them instantly."""
+ import time
+
+ await tools.navigate(url=f'{base_url}/datalist-field', new_tab=False, browser_session=browser_session)
+ await asyncio.sleep(0.3)
+ await browser_session.get_browser_state_summary()
+ city_idx = await browser_session.get_index_by_id('city')
+ assert city_idx is not None
+
+ t0 = time.monotonic()
+ await tools.input(index=city_idx, text='Chi', browser_session=browser_session)
+ duration = time.monotonic() - t0
+
+ # Datalist fields should complete without the 400ms tax.
+ # Normal typing for 3 chars takes well under 400ms.
+ assert duration < 0.4, f'Datalist field got unexpected delay: {duration:.3f}s (should be < 0.4s)'