如何处理木偶戏中的弹出窗口

时间:2022-12-20 19:20:01

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);
}

3 个解决方案

#1


2  

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

#2


1  

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();
}

#3


0  

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.

相关文档可以在这里找到。

#1


2  

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

#2


1  

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();
}

#3


0  

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.

相关文档可以在这里找到。