如何在 playwright 中运行自定义 js 函数

Rob*_*obo 5 javascript webautomation puppeteer chrome-devtools-protocol playwright

如何在 playwright 中运行自定义 js 函数?例如显示警报。我已经尝试过这种方式,但没有奏效。

var url = await page.evaluate(async() => {
  await function alert() {
    alert("alert");
  }

  await alert();
});
Run Code Online (Sandbox Code Playgroud)

pgu*_*rio 7

你会这样做:

await page.evaluate(() => alert("alert"))
Run Code Online (Sandbox Code Playgroud)

但请记住,该警报在 JavaScript 浏览器世界中被阻止并被 Playwright 自动解除。见这里如何与他们互动(如解雇)