Skip to content

Commit 29f0e23

Browse files
committed
updated download logic to work with use_api=false
1 parent 33cea7d commit 29f0e23

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

stagehand/main.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,23 @@ async def init(self):
511511
self.logger,
512512
)
513513
self._playwright_page = self._page._page
514+
515+
# Set up download behavior via CDP
516+
try:
517+
# Create CDP session for the page
518+
cdp_session = await self._context.new_cdp_session(self._playwright_page)
519+
# Enable download behavior
520+
await cdp_session.send("Browser.setDownloadBehavior", {
521+
"behavior": "allow",
522+
"downloadPath": "downloads",
523+
"eventsEnabled": True
524+
})
525+
526+
self.logger.debug(f"Set up CDP download behavior")
527+
except Exception as e:
528+
self.logger.warning(f"Failed to set up CDP download behavior: {str(e)}")
529+
# Continue without download support - non-critical feature
530+
514531
except Exception:
515532
await self.close()
516533
raise
@@ -532,29 +549,6 @@ async def init(self):
532549
)
533550
self._playwright_page = self._page._page
534551

535-
# Set up download behavior via CDP
536-
try:
537-
# Check if downloadsPath is provided in launch options, otherwise use default
538-
downloads_path = self.local_browser_launch_options.get("downloadsPath")
539-
if not downloads_path:
540-
downloads_path = str(Path.cwd() / "downloads")
541-
# Create downloads directory if it doesn't exist
542-
Path(downloads_path).mkdir(parents=True, exist_ok=True)
543-
544-
# Create CDP session for the page
545-
cdp_session = await self._context.new_cdp_session(self._playwright_page)
546-
# Enable download behavior
547-
await cdp_session.send("Browser.setDownloadBehavior", {
548-
"behavior": "allow",
549-
"downloadPath": downloads_path,
550-
"eventsEnabled": True
551-
})
552-
553-
self.logger.debug(f"Set up CDP download behavior for local browser with path: {downloads_path}")
554-
except Exception as e:
555-
self.logger.warning(f"Failed to set up CDP download behavior for local browser: {str(e)}")
556-
# Continue without download support - non-critical feature
557-
558552
except Exception:
559553
await self.close()
560554
raise

0 commit comments

Comments
 (0)