木偶确认

时间:2018-04-09 08:34:26

标签: javascript confirm puppeteer

我正在努力学习木偶戏。我已成功编写了登录页面和一些导航的脚本。然后我点击一个按钮。该页面抛出一​​个window.confirm,我希望我的脚本接受这个继续下一步但我无法弄清楚如何。

有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:3)

刚刚在这里进行了一个简单的测试,当确认对话框出现时。只需按Enter即可关闭对话框。

所以我们在木偶戏中能做的就是做到这一点。 我敲了一个有确认框的快速网页,..

例如

<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只是一个简单的基于promise的setTimeout等待,所以我们有机会出现确认对话框。

如果您目前没有承诺延迟功能,可以使用以下功能。

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

更新:遗憾的是,对话框无法可靠地响应按键。但是,puppeter确实有一个我们可以附加的对话事件。

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

你甚至可以解雇,并阅读发送的消息等。更多信息在这里 - &gt; https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-dialog

答案 1 :(得分:1)

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

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://stackoverflow.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);
})()

答案 2 :(得分:0)

我们正在构建要传递的对象,因此有些不同,但我想我会分享。

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

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

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