Skip to main content
Version: 23.8.0

Browser.waitForTarget() 方法

¥Browser.waitForTarget() method

等待直到出现与给定 predicate 匹配的 target 并返回它。

¥Waits until a target matching the given predicate appears and returns it.

这看起来会是全部打开的 浏览器上下文

¥This will look all open browser contexts.

签名

¥Signature

class Browser {
waitForTarget(
predicate: (x: Target) => boolean | Promise<boolean>,
options?: WaitForTargetOptions,
): Promise<Target>;
}

参数

¥Parameters

范围

类型

描述

predicate

(x: 目标) => 布尔 | Promise<布尔值>

options

WaitForTargetOptions

(可选的)

Returns:

Promise<Target>

示例

¥Example

查找通过 window.open 打开的页面的目标:

¥Finding a target for a page opened via window.open:

await page.evaluate(() => window.open('https://www.example.com/'));
const newWindowTarget = await browser.waitForTarget(
target => target.url() === 'https://www.example.com/',
);