Waiting for download to complete on Puppeteer

368
July 27, 2022, at 06:00 AM

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(); 
})();
Rent Charter Buses Company
READ ALSO
Why is my signed XML getting rejected by authorization?

Why is my signed XML getting rejected by authorization?

I'm trying to create a signed XML post requestThe XML needs to be sent in the body of the request

207
What library can I use to generate random mock data based on Open API spec inside a node application?

What library can I use to generate random mock data based on Open API spec inside a node application?

I am looking for a library which would generate a mock data based on openapi spec (client side only)So the idea is not to have a separate local server, as it's done in openapi-mock-express-middleware, but rather have data to be generated on fly inside...

109
TypeError: models[model].defineConstraints is not a function

TypeError: models[model].defineConstraints is not a function

I`m trying to undestand whats going on and why defineConstraints is not a functioncan someone help me ?

107
use nodejs var as json object statement?

use nodejs var as json object statement?

how do I use a nodejs var inside a json statement, I dont realy have the required vocabulary to explain but here is my simplifyed code:

112