Skip to main content
Version: 25.3.0

Realm.waitForFunction() 方法

🌐 Realm.waitForFunction() method

等待函数在字段的上下文中求值时返回一个真值。

🌐 Waits for a function to return a truthy value when evaluated in the realm's context.

参数可以从 Node.js 传递到 pageFunction

🌐 Arguments can be passed from Node.js to pageFunction.

语法

🌐 Signature

class Realm {
waitForFunction<
Params extends unknown[],
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
>(
pageFunction: Func | string,
options?: {
polling?: 'raf' | 'mutation' | number;
timeout?: number;
root?: ElementHandle<Node>;
signal?: AbortSignal;
},
...args: Params
): Promise<HandleFor<Awaited<ReturnType<Func>>>>;
}

参数

🌐 Parameters

范围

🌐 Parameter

类型

🌐 Type

描述

🌐 Description

pageFunction

函数 | 字符串

🌐 Func | string

在字段中进行评估的函数。

🌐 A function to evaluate in the realm.

options

{ polling?: 'raf' | 'mutation' | number; timeout?: number; root?: ElementHandle<Node>; signal?: AbortSignal; }

(可选) 轮询和超时的选项。

🌐 (Optional) Options for polling and timeouts.

args

参数

🌐 Params

传递给函数的参数。

🌐 Arguments to pass to the function.

返回:

Promise<HandleFor<Awaited<ReturnType<Func>>>>

当函数返回一个真值时解决的承诺。

🌐 A promise that resolves when the function returns a truthy value.

示例

🌐 Example

const selector = '.foo';
await realm.waitForFunction(
selector => !!document.querySelector(selector),
{},
selector,
);