Skip to main content
Version: 24.38.0

Page.waitForResponse() 方法

🌐 Page.waitForResponse() method

语法

🌐 Signature

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

参数

🌐 Parameters

范围

🌐 Parameter

类型

🌐 Type

描述

🌐 Description

urlOrPredicate

string | AwaitablePredicate<HTTPResponse>

要等待的 URL 或谓词。

🌐 A URL or predicate to wait for.

options

WaitTimeoutOptions

(可选) 可选的等待参数

🌐 (Optional) Optional waiting parameters

返回:

Promise<HTTPResponse>

解析为匹配响应的 Promise。

🌐 Promise which resolves to the matched response.

附注

🌐 Remarks

可选参数有:

🌐 Optional Parameter have:

  • timeout:最大等待时间,以毫秒为单位,默认值为 30 秒,传入 0 可禁用超时。可以使用 Page.setDefaultTimeout() 方法更改默认值。
  • signal:一个信号对象,允许你取消 waitForResponse 调用。

示例

🌐 Example

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();