robert0
robert0

Reputation: 445

Repeating the Puppeteer code (a process) more than once

Ok, so let's say I have this code that is currently working great:

(async () => {
  const browser = await puppeteer.launch;
  const page = await browser.newPage();

  await page.setDefaultNavigationTimeout(timeout);
  await page.setViewport({ width: 1000, height: 1000 });
  await page.goto(`https://www.website1.com`);

  // Grab the result
  while (true) {
    const result = await grabResult(page);
    console.log(result);


 //Do something with the above data in website2.

await page.goto(`https://www.website2.com/`);

await page.waitForTimeout(5000);    

  }

})();

I want to add a line, so that I could repeat the same process (above code) more than once.

And have some kind of option to set the process repeat count.

Any kind of help would be appreciated.

Upvotes: 0

Views: 791

Answers (1)

vsemozhebuty
vsemozhebuty

Reputation: 13772

As a variant:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const [page] = await browser.pages();

  let counter = 10;

  while (counter--) {
    await page.goto(url);
    // Other code;

    await page.waitForTimeout(10000);
  }

  await browser.close();
})();

Upvotes: 3

Related Questions