Skip to main content
Version: 23.2.0

Frame.waitForFunction() 方法

¥Frame.waitForFunction() method

签名

¥Signature

class Frame {
waitForFunction<
Params extends unknown[],
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
>(
pageFunction: Func | string,
options?: FrameWaitForFunctionOptions,
...args: Params
): Promise<HandleFor<Awaited<ReturnType<Func>>>>;
}

参数

¥Parameters

范围

类型

描述

pageFunction

功能 | 字符串

在框架上下文中评估的函数。

options

FrameWaitForFunctionOptions

(可选)用于配置轮询方法和超时的选项。

args

参数

要传递给 pageFunction 的参数。

Returns:

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

pageFunction 返回真值时解决的 promise。

¥the promise which resolve when the pageFunction returns a truthy value.

示例

¥Example

waitForFunction 可用于观察视口大小的变化:

¥The waitForFunction can be used to observe viewport size change:

import puppeteer from 'puppeteer';

(async () => {
. const browser = await puppeteer.launch();
. const page = await browser.newPage();
. const watchDog = page.mainFrame().waitForFunction('window.innerWidth < 100');
. page.setViewport({width: 50, height: 50});
. await watchDog;
. await browser.close();
})();

要将参数从 Node.js 传递到 page.waitForFunction 函数的谓词:

¥To pass arguments from Node.js to the predicate of page.waitForFunction function:

const selector = '.foo';
await frame.waitForFunction(
selector => !!document.querySelector(selector),
{}, // empty options object
selector
);