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
formatting
Nick Sweeting16564c751d0e
diff --git a/browser_use/browser/popups_watchdog.py b/browser_use/browser/popups_watchdog.pyindex 8f7543f1d..93d1aae2a 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 AgentFocusChangedEvent, DialogOpenedEvent, TabCreatedEvent+from browser_use.browser.events import DialogOpenedEvent, TabCreatedEventfrom browser_use.browser.watchdog_base import BaseWatchdog@@ -37,19 +37,19 @@ class PopupsWatchdog(BaseWatchdog):self.logger.info(f'π Starting dialog handler setup for target {target_id}')try: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}")-+ 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 itevent = self.browser_session.event_bus.dispatch(DialogOpenedEvent(@@ -60,20 +60,20 @@ class PopupsWatchdog(BaseWatchdog):))await event.event_result(raise_if_none=False, raise_if_any=True, timeout=5.0)-+# Accept the dialog immediately to unblock the browsertry:if self.browser_session._cdp_client_root and session_id:- self.logger.info("π Sending handleJavaScriptDialog command")+ 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")+ self.logger.info('β Dialog accepted successfully')else:- self.logger.error("Cannot accept dialog - CDP client or session not available")+ 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}")+ self.logger.error(f'Failed to accept dialog: {e}')cdp_session.cdp_client.register.Page.javascriptDialogOpening(handle_dialog)self.logger.info(@@ -94,7 +94,6 @@ class PopupsWatchdog(BaseWatchdog):assert self.browser_session.agent_focus is not None, 'Agent focus not set when handling DialogOpenedEvent'-current_focus_url = self.browser_session.agent_focus.urlcurrent_focus_target_id = self.browser_session.agent_focus.target_id@@ -112,9 +111,11 @@ class PopupsWatchdog(BaseWatchdog):timeout=5.0,)# 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})+ 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')-+# 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)