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
fix cached selector map and js popups
Nick Sweetingc440e29ea1e2
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex d820d671e..df8f0d544 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -1494,9 +1494,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):cached_selector_map = self.browser_session._cached_browser_state_summary.dom_state.selector_mapcached_element_hashes = {hash(e) for e in cached_selector_map.values()}else:+ cached_selector_map = {}cached_element_hashes = set()except Exception as e:self.logger.error(f'Error getting cached selector map: {e}')+ cached_selector_map = {}cached_element_hashes = set()# await self.browser_session.remove_highlights()diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.pyindex 7d4de8daf..af145f4df 100644--- a/browser_use/browser/default_action_watchdog.py+++ b/browser_use/browser/default_action_watchdog.py@@ -296,9 +296,32 @@ class DefaultActionWatchdog(BaseWatchdog):except Exception as e:self.logger.debug(f'JavaScript getBoundingClientRect failed: {e}')- # If we still don't have quads, fail+ # If we still don't have quads, fall back to JS clickif not quads:- raise Exception('Could not get element geometry from any method')+ self.logger.warning('⚠️ Could not get element geometry from any method, falling back to JavaScript click')+ try:+ result = await cdp_session.cdp_client.send.DOM.resolveNode(+ params={'backendNodeId': backend_node_id},+ session_id=session_id,+ )+ assert 'object' in result and 'objectId' in result['object'], (+ 'Failed to find DOM element based on backendNodeId, maybe page content changed?'+ )+ object_id = result['object']['objectId']++ await cdp_session.cdp_client.send.Runtime.callFunctionOn(+ params={+ 'functionDeclaration': 'function() { this.click(); }',+ 'objectId': object_id,+ },+ session_id=session_id,+ )+ await asyncio.sleep(0.5)+ # Navigation is handled by BrowserSession via events+ return None+ except Exception as js_e:+ self.logger.error(f'CDP JavaScript click also failed: {js_e}')+ raise Exception(f'Failed to click element: {js_e}')# Find the largest visible quad within the viewportbest_quad = Nonediff --git a/browser_use/browser/popups_watchdog.py b/browser_use/browser/popups_watchdog.pyindex 366b9366a..212f81d89 100644--- a/browser_use/browser/popups_watchdog.py+++ b/browser_use/browser/popups_watchdog.py@@ -6,7 +6,7 @@ from typing import ClassVarfrom bubus import BaseEventfrom pydantic import PrivateAttr-from browser_use.browser.events import DialogOpenedEvent, TabCreatedEvent+from browser_use.browser.events import AgentFocusChangedEvent, DialogOpenedEvent, TabCreatedEventfrom browser_use.browser.watchdog_base import BaseWatchdog@@ -36,19 +36,45 @@ class PopupsWatchdog(BaseWatchdog):self.logger.info(f'📌 Starting dialog handler setup for target {target_id}')try:- # Set up handler for JavaScript dialogs- # CDP handlers are sync but we need to handle dialogs immediately to unblock the browser- def handle_dialog(event_data, _session_id: str | None = None):- self.browser_session.event_bus.dispatch(+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id, focus=False)++ # Set up async handler for JavaScript dialogs - now we can handle them immediately!+ async def handle_dialog(event_data, session_id: str | None = None):+ """Handle JavaScript dialog events - accept immediately and dispatch event."""+ self.logger.info(f"🚨 DIALOG EVENT RECEIVED: {event_data}, session_id={session_id}")++ dialog_type = event_data.get('type', 'alert')+ message = event_data.get('message', '')+ url = event_data.get('url')+ frame_id = event_data.get('frameId')++ self.logger.info(f"🔔 JavaScript {dialog_type} dialog detected: '{message[:50]}...' - accepting immediately")++ # Dispatch the event first so tests can observe it+ event = self.browser_session.event_bus.dispatch(DialogOpenedEvent(- frame_id=event_data.get('frameId'),- dialog_type=event_data.get('type'),- message=event_data.get('message'),- url=event_data.get('url'),+ frame_id=frame_id,+ dialog_type=dialog_type,+ message=message,+ url=url,))+ await event.event_result(raise_if_none=False, raise_if_any=True, timeout=5.0)++ # Accept the dialog immediately to unblock the browser+ try:+ if self.browser_session._cdp_client_root and session_id:+ self.logger.info("🔄 Sending handleJavaScriptDialog command")+ await self.browser_session._cdp_client_root.send.Page.handleJavaScriptDialog(+ params={'accept': True},+ session_id=session_id,+ )+ self.logger.info("✅ Dialog accepted successfully")+ else:+ self.logger.error("Cannot accept dialog - CDP client or session not available")+ except Exception as e:+ self.logger.error(f"Failed to accept dialog: {e}")- cdp_session = await self.browser_session.get_or_create_cdp_session(target_id, focus=False)cdp_session.cdp_client.register.Page.javascriptDialogOpening(handle_dialog)self.logger.info(f'✅ Successfully registered Page.javascriptDialogOpening handler for session {cdp_session.session_id}'@@ -68,11 +94,16 @@ class PopupsWatchdog(BaseWatchdog):assert self.browser_session.agent_focus is not None, 'Agent focus not set when handling DialogOpenedEvent'- cdp_session = await self.browser_session.cdp_client_for_frame(event.frame_id)++ current_focus_url = self.browser_session.agent_focus.url+ current_focus_target_id = self.browser_session.agent_focus.target_id++ cdp_session = await asyncio.wait_for(self.browser_session.cdp_client_for_frame(event.frame_id), timeout=5.0)try:# delay to look more humanawait asyncio.sleep(0.25)assert self.browser_session._cdp_client_root+ self.browser_session._cdp_client_root.register.Page.javascriptDialogClosed(lambda *args: None)await asyncio.wait_for(self.browser_session._cdp_client_root.send.Page.handleJavaScriptDialog(params={'accept': True},@@ -80,20 +111,29 @@ class PopupsWatchdog(BaseWatchdog):),timeout=5.0,)- # CRITICAL: you must either wait for Page.javascriptDialogClosed or wait for some fixed time here- await asyncio.sleep(0.2)- assert (- await cdp_session.cdp_client.send.Runtime.evaluate(- params={'expression': '1+1'}, session_id=cdp_session.session_id- )- )['result'].get('value') == 2, 'Browser crashed after handling JS dialog popup'+ # CRITICAL: you must activate the target after handling the dialog, otherwise the browser will crash 5 seconds later+ await self.browser_session.agent_focus.cdp_client.send.Target.activateTarget(params={'targetId': current_focus_target_id})self.logger.info('✅ JS dialog popup handled successfully')- # self.browser_session.agent_focus = await self.browser_session.get_or_create_cdp_session(current_focus.target_id, focus=True, new_socket=True)- # assert await self.browser_session.agent_focus.cdp_client.send.Page.getFrameTree(session_id=self.browser_session.agent_focus.session_id) is not None, "Agent focus not set after handling dialog"++ # graveyard:+ # # new_target = await self.browser_session._cdp_client_root.send.Target.createTarget(params={'url': current_focus_url})+ # # self.browser_session.agent_focus = await self.browser_session.get_or_create_cdp_session(target_id=new_target.get('targetId'), new_socket=True, focus=True)+ # # raise NotImplementedError('TODO: figure out why this requires a hard refresh and new socket to avoid crashing the entire browser on JS dialogs')+ # await asyncio.sleep(0.2)+ # await asyncio.wait_for(+ # self.browser_session._cdp_client_root.send.Runtime.evaluate(+ # params={'expression': '1'},+ # session_id=cdp_session.session_id,+ # ),+ # timeout=5.0,+ # )+ # # self.browser_session.agent_focus = await self.browser_session.get_or_create_cdp_session(current_focus.target_id, focus=True, new_socket=True)+ # # assert await self.browser_session.agent_focus.cdp_client.send.Page.getFrameTree(session_id=self.browser_session.agent_focus.session_id) is not None, "Agent focus not set after handling dialog"except Exception as e:- self.logger.error(f'Failed to handle JavaScript dialog: {e}')+ self.logger.error(f'Failed to handle JavaScript dialog gracefully: {e}')+ # raise# finally:# self.event_bus.dispatch(AgentFocusChangedEvent(- # tab_index=await self.browser_session.get_tab_index(current_focus.target_id),- # url=current_focus.url,+ # tab_index=0,+ # url=self.browser_session.agent_focus.url,# ))diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 56abec1ef..4a22e8eac 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -403,8 +403,9 @@ class Controller(Generic[Context]):logger.info(f'Found file input closest to scroll position (distance: {min_distance}px)')else:msg = 'No file upload element found on the page'- logger.info(msg)+ logger.error(msg)raise BrowserError(msg)+ # TODO: figure out why this fails sometimes + add fallback hail mary, just look for any file input on page# Dispatch upload file event with the file input nodetry:diff --git a/pyproject.toml b/pyproject.tomlindex 2b65d29ac..87255f8cc 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -2,7 +2,7 @@name = "browser-use"description = "Make websites accessible for AI agents"authors = [{ name = "Gregor Zunic" }]-version = "0.5.11"+version = "0.5.12"readme = "README.md"requires-python = ">=3.11,<4.0"classifiers = [