Downloads
note
For uploading files, see the uploading files section.
For every attachment downloaded by the page, page.on("download") event is emitted. If you create a browser context with the accept_downloads
set, all these attachments are going to be downloaded into a temporary folder. You can obtain the download url, file system path and payload stream using the Download object from the event.
You can specify where to persist downloaded files using the downloads_path
option in browser_type.launch(**kwargs).
note
Unless downloads_path
is set, downloaded files are deleted when the browser context that produced them is closed.
Here is the simplest way to handle the file download:
- Sync
- Async
#
VariationsIf you have no idea what initiates the download, you can still handle the event:
- Sync
- Async
Note that handling the event forks the control flow and makes script harder to follow. Your scenario might end while you are downloading a file since your main control flow is not awaiting for this operation to resolve.