简体   繁体   English

如何处理 poppeteer 中的弹出窗口

[英]How to handle popups in puppeteer

how to handle the popup and access the popup to do some operations on it.如何处理弹出窗口并访问弹出窗口以对其执行一些操作。

const puppeteer = require('puppeteer');

async function run() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://example.com');
    await page.click(Launchpopup);
}

Since version 0.13.0, you can use the following code: 从版本0.13.0开始,您可以使用以下代码:

... code to open popup...
const pages = await browser.pages(); // get all open pages by the browser
const popup = pages[pages.length - 1]; // the popup should be the last page opened

This code is Typescript, but you get the idea: 这段代码是Typescript,但你明白了:

async function waitForPopupMatching(
  browser: Puppeteer.Browser,
  regex: RegExp,
  openAction: () => Promise<void>,
  timeout: number = 30000,
): Promise<Puppeteer.Page> {
  const promise = new Bluebird<Puppeteer.Target>(resolve => {
    const listener = async (target: Puppeteer.Target) => {
      if (target.type() === 'page' && regex.test(target.url())) {
        browser.removeListener('targetcreated', listener);
        resolve(target);
      }
    };
    browser.addListener('targetcreated', listener);
  }).timeout(timeout);

  await openAction(); // Typically a mouse click

  const tgt = await promise;
  return await tgt.page();
}

Puppeteer popup event Puppeteer 弹出事件

Starting with puppeteer version 1.12, a special 'popup' event has been added to the page, which allows you to catch new tabs and popups.从 puppeteer 版本 1.12 开始,页面中添加了一个特殊的“弹出”事件,它允许您捕获新的选项卡和弹出窗口。 We use it by rewriting the first example:我们通过重写第一个例子来使用它:

const puppeteer = require('puppeteer');         // puppeteer

const browser = await puppeteer.launch();       // run browser
const page = await browser.newPage();           // open new tab
await page.goto('https://example.com');            // go to site.com       

await page.waitForSelector('#goto');            // wait object load
const link = await page.$('#goto');             // declare object

const newPagePromise = new Promise(x => page.once('popup', x));

await link.click();                        // click, a new tab  opens
const newPage = await newPagePromise; 
await newPage.close();             // close it, for example
await browser.close();                          // close browser

You should check out the documentation for v0.12.0-alpha, it describes how to interact with dialogs. 您应该查看v0.12.0-alpha的文档,它描述了如何与对话框进行交互。

const puppeteer = require('puppeteer');

puppeteer.launch().then(async browser => {
  const page = await browser.newPage();
  await page.goto('https://example.com');
  page.on('dialog', async dialog => {
    console.log(dialog.message());
    await dialog.dismiss();
    await browser.close();
  });
  page.evaluate(() => alert('1'));
});

Relevant docs can be found here . 相关文档可以在这里找到

So what I do is I login to facebook on their homepage, then navigate to the page I want to go to where the sign in with facebook button is I click on it. 所以我做的是我在他们的主页上登录Facebook,然后导航到我想要去的地方,我点击它登录facebook按钮。 And then this god below will once the popup happens click on the login with facebook button. 然后这个下面的神将弹出一次点击登录与Facebook按钮。

        await page.click('[service_name="facebook"]')
        const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page()))); 
        const popup = await newPagePromise;
        await popup.waitForSelector('[name="__CONFIRM__"]')
        const confirm = await popup.$('[name="__CONFIRM__"]')
        await popup.click('[name="__CONFIRM__"]')
        await page.waitFor(2000);
        await page.goto('your login page'); $

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

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