I need to download several .csv files from a website and save them with different names. However, they all download with the same name. And therefor, they keep overwriting the last one, leaving me with just one file at the end. Here's my code:
for(let i = 0; i <= 10; i++){
await page._client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath: `./csvFiles/`,
});
await page.click("button_selector");
}
The download starts after Puppeteer clicks the download button, and since all this code is in a loop, I can't rename the files because they are only created after the loop is over, by which time I only have one file downloaded.
If the URLs of the CSV files can be obtained, you could insert (or modify) the download
attribute of the <a>
tag.
Otherwise, you could possibly intercept the server's HTTP response and modify the Content-Disposition
response header to set a filename so the browser receives an altered HTTP response with the explicit filename.