Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In puppeteer how to wait for pop up page to finish loading?

Tags:

puppeteer

In the following example how do I wait for the pop up window to finish loading? After clikcing the google icon you get a pop up window to login to gmail, when I try to interact with the second page it is undefined (as I don't know how to wait for it to fully load. Any advice?

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({headless: false});
    page = await browser.newPage();
    await page.goto("https://www.example.com/signin");
    await page.waitForSelector(".Icon-google");
    await page.click(".Icon-google");
    const pages = await browser.pages();
    console.log(pages[2].url());
})();
like image 844
Captain_Meow_Meow Avatar asked May 01 '18 15:05

Captain_Meow_Meow


Video Answer


2 Answers

You can wait for a new target to be created.

const browser = await puppeteer.launch({headless: false});
page = await browser.newPage();
await page.goto("https://app.testim.io/#/signin");
await page.waitForSelector(".Icon-google");
const nav = new Promise(res => browser.on('targetcreated', res))
await page.click(".Icon-google");
await nav
const pages = await browser.pages();
console.log(pages.length);//number of pages increases !
console.log(pages.map(page => page.url()));

P.S. first I tried page.waitForNavigation() but it didn't work, probably because it's a popup.

like image 138
eliranhe Avatar answered Nov 10 '22 15:11

eliranhe


const [newPage] = await Promise.all([
  new Promise((resolve) => page.once('popup', resolve)),
  page.click('something.that-will-open-the-popup')
]);

await newPage.waitForSelector('.page-is-loaded')
like image 44
Raphael Rafatpanah Avatar answered Nov 10 '22 15:11

Raphael Rafatpanah