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

时间:2017-10-10 14:51:44

标签: node.js google-chrome-headless puppeteer

如何处理弹出窗口并访问弹出窗口以对其执行某些操作。

 "prefetch-count" : 1
 "ack-mode"       : "on-confirm"

4 个答案:

答案 0 :(得分:4)

从0.13.0版开始,您可以使用以下代码:

data_dir = '//home//kate//Рабочий стол//барахло линух минт//more_breeds_all_new'

答案 1 :(得分:3)

这段代码是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();
}

答案 2 :(得分:0)

您应该查看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'));
});

相关docs can be found here

答案 3 :(得分:0)

所以我要做的是我在其主页上登录Facebook,然后导航至我要转到的页面,单击“我使用Facebook登录”按钮。然后,一旦弹出窗口出现,下面的这位上帝将单击使用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'); $