mirror of
https://github.com/browser-use/browser-use
synced 2026-05-13 17:56:35 +02:00
formatting
This commit is contained in:
@@ -6,7 +6,7 @@ from typing import ClassVar
|
||||
from bubus import BaseEvent
|
||||
from pydantic import PrivateAttr
|
||||
|
||||
from browser_use.browser.events import AgentFocusChangedEvent, DialogOpenedEvent, TabCreatedEvent
|
||||
from browser_use.browser.events import DialogOpenedEvent, TabCreatedEvent
|
||||
from 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 it
|
||||
event = 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 browser
|
||||
try:
|
||||
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.url
|
||||
current_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)
|
||||
|
||||
Reference in New Issue
Block a user