Home > Enterprise >  Puppeteer page.goto(url) inside looping not waiting until load
Puppeteer page.goto(url) inside looping not waiting until load

Time:01-04

I have urls of pdf file. ex:

const filesUrl= ["http://example.com/animal.pdf", "https://example.com/fruit.pdf", "http://example.com/stars.pdf"]

I want to download that files and save it to my server folder using puppeteer.

What im doing is:

  1. looping filesUrl
  2. first loop im using page.goto(url)
  3. write file using fs.writeFile
  4. next loop

But i have error: [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined

If i look at the browser, the looping is not waiting until the page is fully loaded.

this is my code:

    const filesUrl = await page.$$eval("li.b_algo h2 a", urls => {
        return urls.map(url => url.href)
    })

    // filesUrl = ["http://example.com/animal.pdf", "https://example.com/fruit.pdf", "http://example.com/stars.pdf"]

    for (const fileUrl of filesUrl) {
        try {
            const filepage = await page.goto(fileUrl)

            await fs.writeFile(Math.random()   ".pdf", await filepage.buffer())
        } catch (error) {
            console.log('errorsadasd:', error);
        }
    }


    await browser.close()

CodePudding user response:

The error is not related to the puppeteer, but to the fs.writeFile, which requires a callback as an argument, which is missing, i.e. undefined, hence the error.

Try adding the callback:

    await fs.writeFile(Math.random()   ".pdf", await filepage.buffer(), () => {
        console.log('done writing file');
    });

CodePudding user response:

Try using writeFile, but wrapped in a promise:

import fs from 'fs';
const fsPromises = fs.promises;
const writeFilePromise = fsPromises.writeFile;

await writeFilePromise(Math.random()   ".pdf", await filepage.buffer());
  •  Tags:  
  • Related