puppeteer/docs/api/puppeteer.frame.waitforfunction.md
dependabot[bot] 93e9acc410
chore(deps-dev): Bump the dev-dependencies group with 3 updates (#12101)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Nikolay Vitkov <nvitkov@chromium.org>
2024-03-20 15:03:14 +00:00

1.7 KiB

sidebar_label
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 and timeout.

args

Params

arguments to pass to the pageFunction.

**Returns:**

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

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

Example

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();
})();

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