傀儡确认

Puppeteer confirm

我正在努力学习木偶师。我已经成功编写了登录页面和一些导航的脚本。然后我让它点击一个按钮。该页面抛出一​​个 window.confirm,我希望我的脚本接受它以继续下一步,但我不知道该怎么做。

谁能指出我正确的方向?

刚刚在这里做了一个简单的测试,确认时弹出对话框。只需按回车键即可关闭对话框。

所以我们在 puppeteer 中可以做的就是那样做。 我打开了一个带有确认框的快速网页,..

例如

<div>Before confirm</div>
<script>
  window.confirm("confirm");
  document.write("<div>After Confirm</div>");
</script>

现在我们的人偶脚本。

await delay(1000);
await page.keyboard.press(String.fromCharCode(13));  
await page.screenshot({path: 'screenshot.png'});
await browser.close();

做上面我的截图是

Before confirm
After Confirm

如果按下确认对话框,正是我们所期望的,.. ps。 delay 只是一个简单的基于承诺的 setTimeout 等待,所以我们有机会出现确认对话框。

如果您目前没有承诺延迟功能,这里有一个供您使用。

const delay = (ms) =>
  new Promise((resolve) => setTimeout(resolve, ms));

更新:不幸的是,对话框不能可靠地响应按键。但是 puppeter 确实有一个我们也可以附加的对话事件。

page.on("dialog", (dialog) => {
  console.log("dialog");
  dialog.accept();
});

您甚至可以关闭并阅读发送的消息等。更多信息在这里-> https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-dialog

谢谢,基思!它现在就像一个魅力。如果有人感兴趣,我已将其发布在这里。

https://gist.github.com/mbierman/5b3e671fa4e848eec899ff486d0cdc26

#!/usr/bin/env node

/**
 * @name Reboot Arris modem. 
 *
 * @desc Puppeteer script for rebooting an Arris modem. 
 * since the specific navigation for your modem will vary, this 
 * is more of an example and isn't guaranteed to work for your particular
 * modem. 
 * Many thanks to https://whosebug.com/users/6870228/keith for his help!
 *
 */

const puppeteer = require('puppeteer')
const screenshot = 'arris.png';

/* Enter your user name and password here between the 's */
const USER = '';
const PASS = '';

    const delay = (ms) =>
    new Promise((resolve) => setTimeout(resolve, ms));

(async () => {

    const browser = await puppeteer.launch({headless: true})
    const page = await browser.newPage()

        console.log("Login...");
    await page.goto('http://192.168.0.1/login.asp');
    await page.type('#id_username', USER, { delay: 10 });
    await page.type('input[type="password"]', PASS, { delay: 10 });
    await page.click('[value="Login"]');
        console.log("Going home...");
    await page.goto('http://192.168.0.1/home.asp');
    await page.click('#alertExitButton');
        console.log("Config...");
    await page.goto('http://192.168.0.1/RgConfiguration.asp');
    console.log('Submit request...');
    page.click('input[type="submit"]');
    console.log('Pause...');

    await page.on("dialog", (dialog) => {
    console.log("Dialog is up...");
        delay(1000);
    console.log("Accepted...");
    dialog.accept();
        delay(1000);
    });

        await delay(3000);
        console.log("Exiting.");
    browser.close();
    process.exit(1);
})()

确认提示由 window.confirm 处的全局函数 confirm(<string>) 触发。该函数会冻结脚本的执行,直到给出响应,然后 returns 将其返回给调用者。如果用户接受提示,则返回值将为 true.

由于我们在网页中处理自定义浏览器会话,您可以用任何您想要的内容覆盖全局变量。

所以在运行触发确认的动作之前window运行

await page.evaluate(`window.confirm = () => true`)

然后当页面代码调用confirm()时它会立即得到一个true响应而不显示任何提示。

我们正在构建要传递的对象,所以它有点不同,但我想我会分享。

我们为操作创建一个对象:

type: "operation",
name: "Touch Delete button from tracks-hero-tmp",
description: "Click and confirm delete and verify track cluster is 1 less",
actions: [
  { method: 'waitForSelector', value: '[tg-name=track_hero_delete_btn]' },
  { method: 'focus', value: '[tg-name=track_hero_delete_btn]' },
  { method: 'click', value: '[tg-name=track_hero_delete_btn]', accept_dialog: 'false' },
  { method: 'screenshot' }
],

然后 "accept_dialog" 被设置为一个布尔值,它被传递到一个异步函数中,我们像这样设置对话响应:

  if (accept_dialog) {
    await dialog.accept();
  } else {
    await dialog.dismiss();
  }

希望这对某人有所帮助。 :)