Downloads
Introduction
For every attachment downloaded by the page, Page.onDownload(handler) event is emitted. All these attachments are downloaded into a temporary folder. You can obtain the download url, file name and payload stream using the Download object from the event.
You can specify where to persist downloaded files using the setDownloadsPath option in BrowserType.launch().
Downloaded files are deleted when the browser context that produced them is closed.
Here is the simplest way to handle the file download:
// Wait for the download to start
Download download = page.waitForDownload(() -> {
// Perform the action that initiates download
page.getByText("Download file").click();
});
// Wait for the download process to complete and save the downloaded file somewhere
download.saveAs(Paths.get("/path/to/save/at/", download.suggestedFilename()));
Variations
If you have no idea what initiates the download, you can still handle the event:
page.onDownload(download -> System.out.println(download.path()));
Note that handling the event forks the control flow and makes the 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.
For uploading files, see the uploading files section.