2022-07-05 13:41:43 +00:00
|
|
|
---
|
|
|
|
sidebar_label: Frame.waitForFunction
|
|
|
|
---
|
|
|
|
|
|
|
|
# Frame.waitForFunction() method
|
|
|
|
|
2022-10-24 07:07:05 +00:00
|
|
|
#### Signature:
|
2022-07-05 13:41:43 +00:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
class Frame {
|
|
|
|
waitForFunction<
|
|
|
|
Params extends unknown[],
|
2023-07-17 08:52:54 +00:00
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
|
2022-07-05 13:41:43 +00:00
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
|
|
|
options?: FrameWaitForFunctionOptions,
|
|
|
|
...args: Params
|
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>>;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
2024-03-20 15:03:14 +00:00
|
|
|
<table><thead><tr><th>
|
2022-07-05 13:41:43 +00:00
|
|
|
|
2024-03-20 15:03:14 +00:00
|
|
|
Parameter
|
|
|
|
|
|
|
|
</th><th>
|
|
|
|
|
|
|
|
Type
|
|
|
|
|
|
|
|
</th><th>
|
|
|
|
|
|
|
|
Description
|
|
|
|
|
|
|
|
</th></tr></thead>
|
|
|
|
<tbody><tr><td>
|
|
|
|
|
|
|
|
pageFunction
|
|
|
|
|
|
|
|
</td><td>
|
|
|
|
|
|
|
|
Func \| string
|
|
|
|
|
|
|
|
</td><td>
|
|
|
|
|
|
|
|
the function to evaluate in the frame context.
|
|
|
|
|
|
|
|
</td></tr>
|
|
|
|
<tr><td>
|
|
|
|
|
|
|
|
options
|
|
|
|
|
|
|
|
</td><td>
|
|
|
|
|
|
|
|
[FrameWaitForFunctionOptions](./puppeteer.framewaitforfunctionoptions.md)
|
|
|
|
|
|
|
|
</td><td>
|
|
|
|
|
|
|
|
_(Optional)_ options to configure the polling method and timeout.
|
|
|
|
|
|
|
|
</td></tr>
|
|
|
|
<tr><td>
|
|
|
|
|
|
|
|
args
|
|
|
|
|
|
|
|
</td><td>
|
|
|
|
|
|
|
|
Params
|
|
|
|
|
|
|
|
</td><td>
|
|
|
|
|
|
|
|
arguments to pass to the `pageFunction`.
|
|
|
|
|
|
|
|
</td></tr>
|
|
|
|
</tbody></table>
|
2022-07-05 13:41:43 +00:00
|
|
|
**Returns:**
|
|
|
|
|
|
|
|
Promise<[HandleFor](./puppeteer.handlefor.md)<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:
|
|
|
|
|
|
|
|
```ts
|
2022-12-09 12:57:39 +00:00
|
|
|
import puppeteer from 'puppeteer';
|
2022-07-05 13:41:43 +00:00
|
|
|
|
|
|
|
(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
|
|
|
|
);
|
|
|
|
```
|