Puppeteer 中文文档v25.8.0

Page.waitForResponse() 方法

签名

class Page {
  waitForResponse(
    urlOrPredicate: string | AwaitablePredicate<HTTPResponse>,
    options?: WaitTimeoutOptions,
  ): Promise<HTTPResponse>;
}

参数

参数

类型

说明

urlOrPredicate

string | AwaitablePredicate<HTTPResponse>

要等待的 URL 或谓词。

options

WaitTimeoutOptions

(可选) 可选的等待参数

返回值:

Promise<HTTPResponse>

解析为匹配响应的 Promise。

备注

可选参数包括:

  • timeout: 等待的最长时间(毫秒)。默认为 30 秒,传入 0 可禁用超时。默认值可以通过使用 Page.setDefaultTimeout() 方法来更改。

  • signal: 一个信号对象,允许你取消 waitForResponse 调用。

示例

const firstResponse = await page.waitForResponse(
  'https://example.com/resource',
);
const finalResponse = await page.waitForResponse(
  response =>
    response.url() === 'https://example.com' && response.status() === 200,
);
const finalResponse = await page.waitForResponse(async response => {
  return (await response.text()).includes('<html>');
});
return finalResponse.ok();