我如何等待所有 xhr 请求完成

Tal*_*man 4 playwright

我单击触发 http 请求的按钮\n并且我希望能够在请求完成后关闭页面。

\n

示例:\nAwait page.click(触发http请求的某个按钮)

\n

我已经尝试过:Await page.waitForLoadState()\nWith \xe2\x80\x9cload\xe2\x80\x9d 和 \xe2\x80\x9cnetworkidle\xe2\x80\x9d 并且它不起作用。\n对于加载页面,没有关闭请求完成并等待网络空闲直到超时。

\n

我希望能够等待后台所有的xhr请求完成

\n

Max*_*itt 8

这是网络限制,您不能等待所有请求完成。全部请求是什么?如果每 50ms 就有一个请求怎么办?

在您的情况下,您可能想等待特定请求:

// Note that Promise.all prevents a race condition
// between clicking and waiting for the response.
const [response] = await Promise.all([
  // Waits for the next response with the specified url
  page.waitForResponse('https://example.com/resource'),
  // Triggers the response
  page.click('button.triggers-response'),
]);

// Alternative way with a predicate.
const [response] = await Promise.all([
  // Waits for the next response matching some conditions
  page.waitForResponse(response => response.url() === 'https://example.com' && response.status() === 200),
  // Triggers the response
  page.click('button.triggers-response'),
]);
Run Code Online (Sandbox Code Playgroud)

请参阅此处:https ://playwright.dev/docs/api/class-page#page-wait-for-response


Bru*_*res 6

您可以使用waitForLoadStatenetworkidle它会等待至少 500 毫秒没有网络连接。

await popup.waitForLoadState('networkidle');
Run Code Online (Sandbox Code Playgroud)

请注意,文档不鼓励这样做。