最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run a custom js function in playwright - Stack Overflow

programmeradmin2浏览0评论

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();
});
Share Improve this question edited Aug 3, 2020 at 2:15 painotpi 6,9962 gold badges39 silver badges72 bronze badges asked Aug 2, 2020 at 20:33 Roboman RoboRoboman Robo 6892 gold badges8 silver badges19 bronze badges 3
  • 1 Looks like a stack overflow... won't the alert fn keep calling itself? – Dennis Hackethal Commented Aug 2, 2020 at 20:38
  • @weltschmerz Not sure, I guess if you say so. This is just for example. New to this. Maybe that is why it is not working. Can you share a fix? – Roboman Robo Commented Aug 2, 2020 at 20:44
  • 1 Well, first, I don't think you need any of the await and async keywords except for maybe the very first await (and you're using them wrong anyway, since you're using await on a function declaration). So basically try getting rid of all the async and await statements inside the call to page.evaluate, run it again, and let me know what you see. – Dennis Hackethal Commented Aug 2, 2020 at 21:06
Add a comment  | 

1 Answer 1

Reset to default 18

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.)

发布评论

评论列表(0)

  1. 暂无评论