Skip to content

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

clear selector map cache after scolling so its recalculated

Nick Sweetingcdfd8b9fcc13
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.py
index 22a6bd20d..28cfc003a 100644
--- a/browser_use/browser/default_action_watchdog.py
+++ b/browser_use/browser/default_action_watchdog.py
@@ -173,6 +173,13 @@ class DefaultActionWatchdog(BaseWatchdog):
params={'targetId': self.browser_session.agent_focus.target_id}
)
+ # IMPORTANT: clear the selector map cache even if no navigation has happened!
+ # it's calculated based on visible elements, and if we don't clear it, it will be wrong
+ self.browser_session._cached_browser_state_summary = None
+ self.browser_session._cached_selector_map.clear()
+ if self.browser_session._dom_watchdog:
+ self.browser_session._dom_watchdog.clear_cache()
+
# Log success
self.logger.info(f'📜 Scrolled {event.direction} by {event.amount} pixels')
return None