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
Refactor exception handling in DownloadsWatchdog for improved robustness
Magnus Müller03de8840c947
diff --git a/browser_use/browser/watchdogs/downloads_watchdog.py b/browser_use/browser/watchdogs/downloads_watchdog.pyindex e1a811c62..7a891cede 100644--- a/browser_use/browser/watchdogs/downloads_watchdog.py+++ b/browser_use/browser/watchdogs/downloads_watchdog.py@@ -147,7 +147,7 @@ class DownloadsWatchdog(BaseWatchdog):'suggested_filename': suggested_filename,'handled': False,}- except Exception:+ except (AssertionError, KeyError):pass# Create and track the tasktask = asyncio.create_task(self._handle_cdp_download(event, target_id, session_id))@@ -169,7 +169,7 @@ class DownloadsWatchdog(BaseWatchdog):try:if guid in self._cdp_downloads_info:self._cdp_downloads_info[guid]['handled'] = True- except Exception:+ except (KeyError, AttributeError):passelse:# No local file path provided, local polling in _handle_cdp_download will handle it@@ -180,7 +180,7 @@ class DownloadsWatchdog(BaseWatchdog):# Remote browser: do not touch local filesystem. Fallback to downloadPath+suggestedFilenameinfo = self._cdp_downloads_info.get(guid, {})try:- suggested_filename = info['suggested_filename']+ suggested_filename = info.get('suggested_filename') or (Path(file_path).name if file_path else 'download')downloads_path = str(self.browser_session.browser_profile.downloads_path or '')effective_path = file_path or str(Path(downloads_path) / suggested_filename)file_name = Path(effective_path).name@@ -222,6 +222,9 @@ class DownloadsWatchdog(BaseWatchdog):# Set download behavior to allow downloads and enable eventsdownloads_path = self.browser_session.browser_profile.downloads_path+ if not downloads_path:+ self.logger.warning('[DownloadsWatchdog] No downloads path configured, skipping CDP download setup')+ return# Ensure path is properly expanded (~ -> absolute path)expanded_downloads_path = Path(downloads_path).expanduser().resolve()await cdp_client.send.Browser.setDownloadBehavior(@@ -294,10 +297,9 @@ class DownloadsWatchdog(BaseWatchdog):download_result = Nonedownload_url = event.get('url', '')suggested_filename = event.get('suggestedFilename', 'download')+ guid = event.get('guid', '')try:- guid = event.get('guid', '')-self.logger.debug(f'[DownloadsWatchdog] ⬇️ File download starting: {suggested_filename} from {download_url[:100]}...')self.logger.debug(f'[DownloadsWatchdog] Full CDP event: {event}')@@ -393,7 +395,7 @@ class DownloadsWatchdog(BaseWatchdog):try:if guid in self._cdp_downloads_info:self._cdp_downloads_info[guid]['handled'] = True- except Exception:+ except (KeyError, AttributeError):passself.logger.debug(f'[DownloadsWatchdog] ✅ File download completed via CDP: {suggested_filename} ({file_size} bytes) saved to {expected_path}'@@ -464,7 +466,7 @@ class DownloadsWatchdog(BaseWatchdog):try:if guid in self._cdp_downloads_info:self._cdp_downloads_info[guid]['handled'] = True- except Exception:+ except (KeyError, AttributeError):passreturnexcept Exception as e: