Skip to content

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

Merge origin/main into fix-minimal-issue-7JLZB

Laith Weinberger0536f5808c27
diff --cc browser_use/browser/watchdogs/downloads_watchdog.py
index 34fe9439a,4baf3a75d..5f0f7b541
--- a/browser_use/browser/watchdogs/downloads_watchdog.py
+++ b/browser_use/browser/watchdogs/downloads_watchdog.py
@@@ -743,6 -814,107 +869,7 @@@ class DownloadsWatchdog(BaseWatchdog)
# We just need to wait for it to appear in the downloads directory
expected_path = downloads_dir / suggested_filename
- # Debug: List current directory contents
- self.logger.debug(f'[DownloadsWatchdog] Downloads directory: {downloads_dir}')
- if downloads_dir.exists():
- files_before = list(downloads_dir.iterdir())
- self.logger.debug(f'[DownloadsWatchdog] Files before download: {[f.name for f in files_before]}')
-
- # Try manual JavaScript fetch as a fallback for local browsers (disabled for regular local downloads)
- if self.browser_session.is_local and self._use_js_fetch_for_local:
- self.logger.debug(f'[DownloadsWatchdog] Attempting JS fetch fallback for {download_url}')
-
- unique_filename = None
- file_size = None
- download_result = None
- try:
- # Escape the URL for JavaScript
- import json
-
- escaped_url = json.dumps(download_url)
-
- # Get the proper session for the frame that initiated the download
- cdp_session = await self.browser_session.cdp_client_for_frame(event.get('frameId'))
- assert cdp_session
-
- result = await cdp_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': f"""
- (async () => {{
- try {{
- const response = await fetch({escaped_url});
- if (!response.ok) {{
- throw new Error(`HTTP error! status: ${{response.status}}`);
- }}
- const blob = await response.blob();
- const arrayBuffer = await blob.arrayBuffer();
- const uint8Array = new Uint8Array(arrayBuffer);
- return {{
- data: Array.from(uint8Array),
- size: uint8Array.length,
- contentType: response.headers.get('content-type') || 'application/octet-stream'
- }};
- }} catch (error) {{
- throw new Error(`Fetch failed: ${{error.message}}`);
- }}
- }})()
- """,
- '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
- # 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(
- guid=guid,
- 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,
- )
- )
- # Mark as handled to prevent duplicate dispatch from progress/polling paths
- try:
- if guid in self._cdp_downloads_info:
- self._cdp_downloads_info[guid]['handled'] = True
- except (KeyError, AttributeError):
- pass
- 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:
return