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
chore: merge main
reformedotcddaf1ed1fd9
diff --cc browser_use/browser/downloads_watchdog.pyindex b583c17d5,71037a356..2ed07cab0--- a/browser_use/browser/downloads_watchdog.py+++ b/browser_use/browser/downloads_watchdog.py@@@ -163,18 -162,7 +163,18 @@@ class DownloadsWatchdog(BaseWatchdog)# Register download event handlersasync def download_will_begin_handler(event: DownloadWillBeginEvent, session_id: SessionID | None):- self.logger.info(f'[DownloadsWatchdog] Download will begin: {event}')+ self.logger.debug(f'[DownloadsWatchdog] Download will begin: {event}')+ # Cache info for later completion event handling (esp. remote browsers)+ guid = event.get('guid', '')+ try:+ suggested_filename = event.get('suggestedFilename')+ assert suggested_filename, 'CDP DownloadWillBegin missing suggestedFilename'+ self._cdp_downloads_info[guid] = {+ 'url': event.get('url', ''),+ 'suggested_filename': suggested_filename,+ }+ except Exception:+ pass# Create and track the tasktask = asyncio.create_task(self._handle_cdp_download(event, target_id, session_id))self._cdp_event_tasks.add(task)@@@ -185,39 -173,10 +185,39 @@@# Check if download is completeif event.get('state') == 'completed':file_path = event.get('filePath')- if file_path:- self.logger.debug(f'[DownloadsWatchdog] Download completed: {file_path}')- # Track the download- self._track_download(file_path)+ guid = event.get('guid', '')+ if self.browser_session.is_local:+ if file_path:- self.logger.info(f'[DownloadsWatchdog] Download completed: {file_path}')++ self.logger.debug(f'[DownloadsWatchdog] Download completed: {file_path}')+ # Track the download+ self._track_download(file_path)+ else:+ # No local file path provided, local polling in _handle_cdp_download will handle it+ self.logger.debug(+ '[DownloadsWatchdog] No filePath in progress event (local); polling will handle detection'+ )+ else:+ # Remote browser: do not touch local filesystem. Fallback to downloadPath+suggestedFilename+ info = self._cdp_downloads_info.get(guid, {})+ try:+ suggested_filename = info['suggested_filename']+ 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+ file_ext = Path(file_name).suffix.lower().lstrip('.')+ self.event_bus.dispatch(+ FileDownloadedEvent(+ url=info.get('url', ''),+ path=str(effective_path),+ file_name=file_name,+ file_size=0,+ file_type=file_ext if file_ext else None,+ )+ )- self.logger.info(f'[DownloadsWatchdog] ✅ (remote) Download completed: {effective_path}')++ self.logger.debug(f'[DownloadsWatchdog] ✅ (remote) Download completed: {effective_path}')+ finally:+ if guid in self._cdp_downloads_info:+ del self._cdp_downloads_info[guid]# Register the handlers with CDPcdp_client.register.Browser.downloadWillBegin(download_will_begin_handler) # type: ignore[arg-type]@@@ -326,58 -282,61 +326,58 @@@}}}})()""",- 'awaitPromise': True,- 'returnByValue': True,- },- session_id=cdp_session.session_id,- )- download_result = result.get('result', {}).get('value')-- if download_result and download_result.get('data'):- # Save the file- file_data = bytes(download_result['data'])- file_size = len(file_data)-- # Ensure unique filename- unique_filename = await self._get_unique_filename(str(downloads_dir), suggested_filename)- final_path = downloads_dir / unique_filename-- # Write the file- import anyio-- async with await anyio.open_file(final_path, 'wb') as f:- await f.write(file_data)-- self.logger.debug(f'[DownloadsWatchdog] ✅ Downloaded and saved file: {final_path} ({file_size} bytes)')- expected_path = final_path- else:- self.logger.error('[DownloadsWatchdog] ❌ No data received from fetch')- # Fall through to polling logic-- except Exception as fetch_error:- self.logger.error(f'[DownloadsWatchdog] ❌ Failed to download file via fetch: {fetch_error}')- # Fall through to polling logic-- # Determine file type from extension- file_ext = expected_path.suffix.lower().lstrip('.')- file_type = file_ext if file_ext else None-- # Emit download event- self.event_bus.dispatch(- FileDownloadedEvent(- url=download_url,- path=str(expected_path),- file_name=unique_filename,- file_size=file_size,- file_type=file_type,- mime_type=download_result.get('contentType') if download_result else None,- from_cache=False,- auto_download=False,- )- )-- self.logger.debug(- f'[DownloadsWatchdog] ✅ File download completed via CDP: {suggested_filename} ({file_size} bytes) saved to {expected_path}'- )- # Success - return early- return+ 'awaitPromise': True,+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ download_result = result.get('result', {}).get('value')++ if download_result and download_result.get('data'):+ # Save the file+ file_data = bytes(download_result['data'])+ file_size = len(file_data)++ # Ensure unique filename+ unique_filename = await self._get_unique_filename(str(downloads_dir), suggested_filename)+ final_path = downloads_dir / unique_filename++ # Write the file+ import anyio++ async with await anyio.open_file(final_path, 'wb') as f:+ await f.write(file_data)+- self.logger.info(f'[DownloadsWatchdog] ✅ Downloaded and saved file: {final_path} ({file_size} bytes)')++ self.logger.debug(f'[DownloadsWatchdog] ✅ Downloaded and saved file: {final_path} ({file_size} bytes)')+ expected_path = final_path+ # Emit download event immediately+ file_ext = expected_path.suffix.lower().lstrip('.')+ file_type = file_ext if file_ext else None+ self.event_bus.dispatch(+ FileDownloadedEvent(+ url=download_url,+ path=str(expected_path),+ file_name=unique_filename or expected_path.name,+ file_size=file_size or 0,+ file_type=file_type,+ mime_type=(download_result.get('contentType') if download_result else None),+ from_cache=False,+ auto_download=False,+ )+ )- self.logger.info(++ self.logger.debug(+ f'[DownloadsWatchdog] ✅ File download completed via CDP: {suggested_filename} ({file_size} bytes) saved to {expected_path}'+ )+ return+ else:+ self.logger.error('[DownloadsWatchdog] ❌ No data received from fetch')++ except Exception as fetch_error:+ self.logger.error(f'[DownloadsWatchdog] ❌ Failed to download file via fetch: {fetch_error}')++ # For remote browsers, don't poll local filesystem; downloadProgress handler will emit the event+ if not self.browser_session.is_local:+ returnexcept Exception as e:self.logger.error(f'[DownloadsWatchdog] ❌ Error handling CDP download: {type(e).__name__} {e}')