跳到主要内容

事件

简介

Playwright 允许监听网页上发生的各种类型的事件,例如网络请求、子页面的创建、专用工作线程等。有几种订阅此类事件的方法,例如等待事件或添加或删除事件监听器。

等待事件

大多数情况下,脚本需要等待特定事件发生。以下是一些典型的等待事件模式。

使用 Page.RunAndWaitForRequestAsync() 等待具有指定 URL 的请求:

var waitForRequestTask = page.WaitForRequestAsync("**/*logo*.png");
await page.GotoAsync("https://wikipedia.org");
var request = await waitForRequestTask;
Console.WriteLine(request.Url);

等待弹出窗口:

var popup = await page.RunAndWaitForPopupAsync(async =>
{
await page.GetByText("open the popup").ClickAsync();
});
await popup.GotoAsync("https://wikipedia.org");

添加/删除事件监听器

有时,事件会在随机时间发生,与其等待它们,不如对其进行处理。Playwright 支持传统的语言机制来订阅和取消订阅事件:

page.Request += (_, request) => Console.WriteLine("Request sent: " + request.Url);
void listener(object sender, IRequest request)
{
Console.WriteLine("Request finished: " + request.Url);
};
page.RequestFinished += listener;
await page.GotoAsync("https://wikipedia.org");

// 删除之前添加的监听器。
page.RequestFinished -= listener;
await page.GotoAsync("https://www.openstreetmap.org/");