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
Enhance DownloadsWatchdog to expand and resolve download paths
Magnus Müllera69450129419
diff --git a/browser_use/browser/watchdogs/downloads_watchdog.py b/browser_use/browser/watchdogs/downloads_watchdog.pyindex a3eae8da7..e1a811c62 100644--- a/browser_use/browser/watchdogs/downloads_watchdog.py+++ b/browser_use/browser/watchdogs/downloads_watchdog.py@@ -60,8 +60,9 @@ class DownloadsWatchdog(BaseWatchdog):# Ensure downloads directory existsdownloads_path = self.browser_session.browser_profile.downloads_pathif downloads_path:- Path(downloads_path).mkdir(parents=True, exist_ok=True)- self.logger.debug(f'[DownloadsWatchdog] Ensured downloads directory exists: {downloads_path}')+ expanded_path = Path(downloads_path).expanduser().resolve()+ expanded_path.mkdir(parents=True, exist_ok=True)+ self.logger.debug(f'[DownloadsWatchdog] Ensured downloads directory exists: {expanded_path}')async def on_TabCreatedEvent(self, event: TabCreatedEvent) -> None:"""Monitor new tabs for downloads."""@@ -221,10 +222,12 @@ class DownloadsWatchdog(BaseWatchdog):# Set download behavior to allow downloads and enable eventsdownloads_path = self.browser_session.browser_profile.downloads_path+ # Ensure path is properly expanded (~ -> absolute path)+ expanded_downloads_path = Path(downloads_path).expanduser().resolve()await cdp_client.send.Browser.setDownloadBehavior(params={'behavior': 'allow',- 'downloadPath': str(downloads_path), # Convert Path to string+ 'downloadPath': str(expanded_downloads_path), # Use expanded absolute path'eventsEnabled': True,})@@ -275,10 +278,14 @@ class DownloadsWatchdog(BaseWatchdog):self, event: DownloadWillBeginEvent, target_id: TargetID, session_id: SessionID | None) -> None:"""Handle a CDP Page.downloadWillBegin event."""- downloads_dir = Path(- self.browser_session.browser_profile.downloads_path- or f'{tempfile.gettempdir()}/browser_use_downloads.{str(self.browser_session.id)[-4:]}'- )+ downloads_dir = (+ Path(+ self.browser_session.browser_profile.downloads_path+ or f'{tempfile.gettempdir()}/browser_use_downloads.{str(self.browser_session.id)[-4:]}'+ )+ .expanduser()+ .resolve()+ ) # Ensure path is properly expanded# Initialize variables that may be used outside try blocksunique_filename = Nonediff --git a/examples/features/download_file.py b/examples/features/download_file.pyindex 2ac115135..edaf15870 100644--- a/examples/features/download_file.py+++ b/examples/features/download_file.py@@ -18,12 +18,12 @@ if not api_key:llm = ChatGoogle(model='gemini-2.5-flash', api_key=api_key)-browser_session = BrowserSession(browser_profile=BrowserProfile(downloads_path='~/Downloads'))+browser_session = BrowserSession(browser_profile=BrowserProfile(downloads_path='~/Downloads/tmp'))async def run_download():agent = Agent(- task='Go to "https://file-examples.com/" and download the smallest doc file.',+ task='Go to "https://file-examples.com/" and download the smallest doc file. then go back and get the next file.',llm=llm,browser_session=browser_session,)