Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

node.js - 如何在Pupeteer中单击按钮时处理新页面?(how to handle new page on button click in pupeteer?)

i am using puppeteer in a project to test a web page , in the page i have several buttons that open a new tab in the browser , how can i handle that using puppeteer?

(我在一个项目中使用puppeteer来测试网页,在该页面中,我有几个按钮可以在浏览器中打开一个新选项卡,我如何使用puppeteer来处理呢?)

const puppeteer = require('puppeteer');

(async() => {
    const browser = await puppeteer.launch({ defaultViewport: null });

    const page = await browser.newPage();

    // go to the URL
    await page.goto('https://example.com/', {waitUntil: 'networkidle'});

    await page.click('.btnId'); //opens new tab with Page 2

    // handle Page 2
    // process Page 2
    // close Page 2

    // go back to Page 1

    browser.close();

})();

how can i handle the page 2 ?

(我该如何处理第2页?)

  ask by MirzaPayam translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
await page.waitFor(3 * 1000)  // wait for new page to open
const pages = await browser.pages()  // get all pages
const page2 = pages[pages.length - 1]  // get the new page
// process the new page
await page2.close()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...