繁体   English   中英

如何从 Puppeteer 中的按钮打开新的 window 页面?

[英]How do I open a new window page from a button in Puppeteer?

我正在尝试通过 Puppeteer 中的按钮打开一个新的 window 页面。

给出的示例:我正在登录一个网站,当我单击登录按钮时,将弹出一个新的 window 页面,重定向到该按钮要访问的站点。 我该怎么做?

您可以通过在执行page.click时简单地按 Shift 按钮来执行此操作,并且要捕获新打开的 window,您可以使用 waitForTarget。

const puppeteer = require('puppeteer')

;(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        defaultViewport: null,
    })
    const context = browser.defaultBrowserContext()
    const page = (await context.pages())[0]
    await page.goto('https://www.amazon.com/gp/product/B093GQSVPX/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1', {waitUntil: 'load'})
    await page.waitForSelector('a[title="Add to List"]', {visible: true})
    await page.keyboard.down('Shift')
    await page.click('a[title="Add to List"]')
    await page.keyboard.up('Shift')
    const popup = await browser.waitForTarget(
        (target) => target.url().includes('www.amazon.com/ap/signin')
    )

    const popupPage = await popup.page()
    await popupPage.waitForSelector('a.a-link-expander[role="button"]')
    await popupPage.click('a.a-link-expander[role="button"]')
    await popupPage.click('input#continue[type="submit"]')
    await browser.close()
})()

我在单击任何内容或使用命令对新打开的 window 执行某些操作时遇到问题,上面的命令一切正常,它返回 popupPage.url() 的值,因此它检测到它已打开,但它会转到操作线.. 并且没有错误,window 上什么也没有发生,有什么建议吗?

await page.goto('https://twitter.com');

  await sleep(3000);
  const frame = page.frames().find(f => f.url().startsWith('https://accounts.google.com/gsi/button'));
  const acceptBtn = await frame.$('#container > div > div.nsm7Bb-HzV7m-LgbsSe-bN97Pc-sM5MNb.oXtfBe-l4eHX > span.nsm7Bb-HzV7m-LgbsSe-BPrWId');
  await acceptBtn.click();

  await sleep(5000);
  const popup = await browser.waitForTarget((target) => target.url().includes('accounts.google.com/v3/signin'));
  //await popup.click('#yDmH0d > c-wiz > div > div.eKnrVb > div > div.j663ec > div > form > span > section > div > div > div:nth-child(3) > button');
  const popupPage = await popup.page()
  console.log(popupPage.url());
  await popupPage.type('input[type="email"]', "hehehe", { delay: 200 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM