问题
I am trying to learn puppeteer. I have successfully scripted a login to a page and some navigation. Then I have it click on a button. The page throws up a window.confirm and I want my script to accept this to continue to the next step but I can’t figure out how.
Can anyone point me in the right direction?
回答1:
Just done a simple test here, when a dialog box comes up on confirm. Simply pressing enter will close the dialog.
So what we can do in puppeteer, is do exactly that. I knocked up a quick webpage that had a confirm box,..
eg.
<div>Before confirm</div>
<script>
window.confirm("confirm");
document.write("<div>After Confirm</div>");
</script>
Now our puppeteer script.
await delay(1000);
await page.keyboard.press(String.fromCharCode(13));
await page.screenshot({path: 'screenshot.png'});
await browser.close();
Doing the above my screenshot is
Before confirm
After Confirm
Exactly what we expect if pressing the confirm dialog,..
ps. delay
is just a simple promises based setTimeout to wait, so we have chance for the confirm dialog to appear.
If you currently don't have a promise delay function, here is one for you to use.
const delay = (ms) =>
new Promise((resolve) => setTimeout(resolve, ms));
UPDATE: Unfortunately dialogs don't respond the keypress reliably. But puppeter does have a dialog event we can attach too.
page.on("dialog", (dialog) => {
console.log("dialog");
dialog.accept();
});
You can even dismiss, and read what message etc was sent. more info here-> https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-dialog
回答2:
Thanks, Keith! It is now working like a charm. If anyone is interested I have posted it here.
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);
})()
来源:https://stackoverflow.com/questions/49728669/puppeteer-confirm