I have a script made using node.js and puppeteer which downloads a file from a button (which doesn't redirect to a url), so right now i'm using await await page.waitForTimeout(1000);
to wait for the download to complete but it has a few flaws, such as:
Depending on the connection, the download might take more than 1000ms to finish, as well as it might take less, which wouldn't make sense to wait more than what took to finish the download.
My question is, is there a way to wait for a download to complete using Node+Puppeteer? I have tried using waitUntil: 'networkidle0
and networkidle2 but both seem to wait forever.
Code below:
const path = require('path');
const puppeteer = require('puppeteer');
(async () => {
/* Initialize some variables */
const browser = await puppeteer.launch();
// Instantiates a new page
const page = await browser.newPage();
// Gets current path
const downloadPath = path.resolve('./');
// Specifies wether it allows downloading multiple files or not
await page._client.send('Page.setDownloadBehavior',
{behavior: 'allow', downloadPath: downloadPath});
// Goes to My Website
await page.goto('http://localhost:8080/mywebsite');
// Exports to CSV
await page.waitForSelector("#W0009EXPORTAXLS > a > i", {visible: true});
await page.tap("#W0009EXPORTAXLS > a > i");
await page.waitForTimeout(1000);
// Log
console.log('File exported.');
// Closes the browser
await browser.close();
})();