在 puppeteer 中如何等待弹出页面完成加载? [英] In puppeteer how to wait for pop up page to finish loading?
本文介绍了在 puppeteer 中如何等待弹出页面完成加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的例子中,我如何等待弹出窗口完成加载?点击谷歌图标后,当我尝试进行交互时,您会看到一个弹出窗口来登录 Gmail第二页是未定义的(因为我不知道如何等待它完全加载.有什么建议吗?
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());
})();
推荐答案
您可以等待创建新目标.
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()));
附言首先我尝试了 page.waitForNavigation()
但它没有用,可能是因为它是一个弹出窗口.
P.S. first I tried page.waitForNavigation()
but it didn't work, probably because it's a popup.
这篇关于在 puppeteer 中如何等待弹出页面完成加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文