Reputation: 115
I'm at the end of a Puppeteer script. I just need to click the OK button on the confirm dialog box (see link to image) or press the enter key. For pressing the enter key I tried every suggestion here Pressing Enter button in puppeteer and nothing worked. I checked with a normal browser and pressing the enter key works. Any suggestions?
Problem solved thanks to Thomas! See solution below.
Upvotes: 8
Views: 7005
Reputation: 53
Note that another way that I used on puppeteer sharp is to duckpunch (redefine) the alert dialog as follows:
Log(robot, "js duckpunch alert dialog ");
var jsExec = @"
function alert(msg) {
console.log('dialog: '+msg)
}
";
await ExecJsExpression(page, jsExec, settings.robot);
Log(robot, "js duckpunch alert dialog done ");
...
where Log is an audit log function of some sort. The variable robot can be ignored, it's just holding state in my example.
...
and ExecJsExpression is as follows:
private static async Task ExecJsExpression(Page page, string jsExec, RobotState robot) {
Log(robot, "js exec [" + jsExec + "]");
Log(robot, "js exec cleaned [" + jsExec + "]");
try {
await page.EvaluateExpressionAsync(jsExec);
Log(robot, "js exec [" + jsExec + "] ok");
} catch (Exception e) {
Log(robot, "Failed to exec js expr [" + jsExec + "]");
throw;
}
}
Upvotes: 0
Reputation: 25220
This is a confirm dialog. Have a look at dialog handling.
You can press the OK button like this:
page.on('dialog', async dialog => {
await dialog.accept();
});
Put the code in front of the action that triggers the dialog (otherwise the event handler will not be registered when the dialog event is fired).
Upvotes: 12