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 | (可选)用于配置轮询方法、超时和信号的选项。 🌐 (Optional) options to configure the polling method, timeout and signal. | |
args | 参数 🌐 Params | 传递给 🌐 arguments to pass to the |
返回:
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,
);