How can I run a custom js function in playwright? For example show an alert. I have already tried this way but it did not work.
var url = await page.evaluate(async() => {
await function alert() {
alert("alert");
}
await alert();
});
How can I run a custom js function in playwright? For example show an alert. I have already tried this way but it did not work.
var url = await page.evaluate(async() => {
await function alert() {
alert("alert");
}
await alert();
});
You would just do:
await page.evaluate(() => alert("alert"))
But keep in mind, that alert's are blocking in the JavaScript browser world and dismissed automatically by Playwright. See here how to interact with them (dismiss e.g.)
© 2022 - 2024 — McMap. All rights reserved.
alert
fn keep calling itself? – Ionaawait
andasync
keywords except for maybe the very firstawait
(and you're using them wrong anyway, since you're usingawait
on a function declaration). So basically try getting rid of all theasync
andawait
statements inside the call topage.evaluate
, run it again, and let me know what you see. – Iona