puppeteer/docs/api/puppeteer.frame.waitforfunction.md
jrandolf f07ad2c661
fix: update documentation on configuring puppeteer (#9150)
This PR updates the docs regarding configuring puppeteer. In addition,
some changes have been made to the documentation generator to show
default values on the documentation site.

Also fixes: https://github.com/puppeteer/puppeteer/pull/9144
2022-10-24 09:07:05 +02:00

63 lines
2.0 KiB
Markdown

---
sidebar_label: Frame.waitForFunction
---
# Frame.waitForFunction() method
#### Signature:
```typescript
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](./puppeteer.framewaitforfunctionoptions.md) | <i>(Optional)</i> options to configure the polling method and timeout. |
| args | Params | arguments to pass to the <code>pageFunction</code>. |
**Returns:**
Promise&lt;[HandleFor](./puppeteer.handlefor.md)&lt;Awaited&lt;ReturnType&lt;Func&gt;&gt;&gt;&gt;
the promise which resolve when the `pageFunction` returns a truthy value.
## Example
The `waitForFunction` can be used to observe viewport size change:
```ts
const puppeteer = require('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:
```ts
const selector = '.foo';
await frame.waitForFunction(
selector => !!document.querySelector(selector),
{}, // empty options object
selector
);
```