Skip to main content
Version: 24.38.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

范围

🌐 Parameter

类型

🌐 Type

描述

🌐 Description

pageFunction

函数 | 字符串

🌐 Func | string

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

🌐 the function to evaluate in the frame context.

options

FrameWaitForFunctionOptions

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

🌐 (Optional) options to configure the polling method, timeout and signal.

args

参数

🌐 Params

传递给 pageFunction 的参数。

🌐 arguments to pass to the pageFunction.

返回:

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

pageFunction 返回一个真值时,承诺会被解决。

🌐 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';

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