跳至主要內容
版本: 23.11.1

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() 方法變更。

範例

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