Skip to content

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

DownloadsWatchdog watches BrowserStateRequestEvent to download PDFs

Cyrusf82f42319cd2
diff --git a/browser_use/browser/watchdogs/downloads_watchdog.py b/browser_use/browser/watchdogs/downloads_watchdog.py
index 7a891cede..398ed8dbe 100644
--- a/browser_use/browser/watchdogs/downloads_watchdog.py
+++ b/browser_use/browser/watchdogs/downloads_watchdog.py
@@ -16,6 +16,7 @@ from pydantic import PrivateAttr
from browser_use.browser.events import (
BrowserLaunchEvent,
+ BrowserStateRequestEvent,
BrowserStoppedEvent,
FileDownloadedEvent,
NavigationCompleteEvent,
@@ -34,6 +35,7 @@ class DownloadsWatchdog(BaseWatchdog):
# Events this watchdog listens to (for documentation)
LISTENS_TO: ClassVar[list[type[BaseEvent[Any]]]] = [
BrowserLaunchEvent,
+ BrowserStateRequestEvent,
BrowserStoppedEvent,
TabCreatedEvent,
TabClosedEvent,
@@ -81,6 +83,26 @@ class DownloadsWatchdog(BaseWatchdog):
"""Stop monitoring closed tabs."""
pass # No cleanup needed, browser context handles target lifecycle
+ async def on_BrowserStateRequestEvent(self, event: BrowserStateRequestEvent) -> None:
+ """Handle browser state request events."""
+ cdp_session = self.browser_session.agent_focus
+ if not cdp_session:
+ return
+
+ url = await self.browser_session.get_current_page_url()
+ if not url:
+ return
+
+ target_id = cdp_session.target_id
+ self.event_bus.dispatch(
+ NavigationCompleteEvent(
+ event_type="NavigationCompleteEvent",
+ url=url,
+ target_id=target_id,
+ event_parent_id=event.event_id,
+ )
+ )
+
async def on_BrowserStoppedEvent(self, event: BrowserStoppedEvent) -> None:
"""Clean up when browser stops."""
# Cancel all CDP event handler tasks