0

Just getting started with Puppeteer and i can launch the browser, go to a url, run a bunch of actions and then close the browser. What i am looking to see if i can do though is open the browser and loop over a set of actions in the same session.

I have a JSON object with urls i want to visit, so want to loop over that

// teams.js
module.exports = {
  premier_league: [
    { team_name: "Team 1", url: "https://url-of-site/team_1"},
    { team_name: "Team 2", url: "https://url-of-site/team_2"}
  ]
}

My script to launch puppeteer is as follows

// index.js
const TEAM = require('./teams');
const puppeteer = require('puppeteer');

(async () => {
  // Initialise Browser
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.setViewport({
    width: 1280,
    height: 800
  });
  await page.goto('login page');

  await page.click('login_box');
  await page.keyboard.type('username');

  await page.click('login_password');
  await page.keyboard.type('password');

  await page.click('login_button');
  await page.waitForNavigation();

  // Go To Team URL
  await page.goto('Team URL')

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

So to loop over my JSON object I can use

Object.keys(TEAM['premier_league']).forEach(function(key) {

  // Output url of each team
  console.log(TEAM['premier_league'][key]['url'])

});

If i wrap my go to url with my loop, then page is no longer accessible

// index.js
const TEAM = require('./teams');
const puppeteer = require('puppeteer');

(async () => {
  // Initialise Browser
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.setViewport({
    width: 1280,
    height: 800
  });
  await page.goto('login page');

  await page.click('login_box');
  await page.keyboard.type('username');

  await page.click('login_password');
  await page.keyboard.type('password');

  await page.click('login_button');
  await page.waitForNavigation();

  Object.keys(TEAM['premier_league']).forEach(function(key) {

    // Go To Team URL
    await page.goto(TEAM['premier_league'][key]['url'])

  });

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

The actual error is

await page.goto(TEAM[args][key]['url'])
    ^^^^

SyntaxError: Unexpected identifier

1 Answer 1

1

Your Object.keys callback function need to use async as well in order to use await inside. Try to change as below

Object.keys(TEAM['premier_league']).forEach( async function(key) {

    // Go To Team URL
    await page.goto(TEAM['premier_league'][key]['url'])

});

Hope it helps

Sign up to request clarification or add additional context in comments.

3 Comments

ah ok, thanks very much, good news is that i can log out the urls but as far as visiting the actual url i get Unhandled promise rejection (rejection id: 1): Error: Protocol error (Page.navigate): Target closed.
Put your async function into a try catch closure and check the error. Seems your page.goto() is failed for some reason
I would not do .forEach with async, it will not await the overall Object.keys loop so the code execution will continue. For sequential async/await, it is easier to use for (const ... of ...) { // await stuff }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.