Skip to content

Commit a0f8a45

Browse files
authored
DownloadsWatchdog watches BrowserStateRequestEvent to download PDFs (#3029)
CC @sauravpanda <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic DownloadsWatchdog now listens for BrowserStateRequestEvent and emits a NavigationCompleteEvent with the current URL and target. This makes PDF downloads reliable after state checks or when there hasn’t been a new navigation. <!-- End of auto-generated description by cubic. -->
2 parents 5f6400d + 036401c commit a0f8a45

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

browser_use/browser/watchdogs/downloads_watchdog.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from browser_use.browser.events import (
1818
BrowserLaunchEvent,
19+
BrowserStateRequestEvent,
1920
BrowserStoppedEvent,
2021
FileDownloadedEvent,
2122
NavigationCompleteEvent,
@@ -34,6 +35,7 @@ class DownloadsWatchdog(BaseWatchdog):
3435
# Events this watchdog listens to (for documentation)
3536
LISTENS_TO: ClassVar[list[type[BaseEvent[Any]]]] = [
3637
BrowserLaunchEvent,
38+
BrowserStateRequestEvent,
3739
BrowserStoppedEvent,
3840
TabCreatedEvent,
3941
TabClosedEvent,
@@ -81,6 +83,26 @@ async def on_TabClosedEvent(self, event: TabClosedEvent) -> None:
8183
"""Stop monitoring closed tabs."""
8284
pass # No cleanup needed, browser context handles target lifecycle
8385

86+
async def on_BrowserStateRequestEvent(self, event: BrowserStateRequestEvent) -> None:
87+
"""Handle browser state request events."""
88+
cdp_session = self.browser_session.agent_focus
89+
if not cdp_session:
90+
return
91+
92+
url = await self.browser_session.get_current_page_url()
93+
if not url:
94+
return
95+
96+
target_id = cdp_session.target_id
97+
self.event_bus.dispatch(
98+
NavigationCompleteEvent(
99+
event_type='NavigationCompleteEvent',
100+
url=url,
101+
target_id=target_id,
102+
event_parent_id=event.event_id,
103+
)
104+
)
105+
84106
async def on_BrowserStoppedEvent(self, event: BrowserStoppedEvent) -> None:
85107
"""Clean up when browser stops."""
86108
# Cancel all CDP event handler tasks

0 commit comments

Comments
 (0)