waitForFunction method

Future<JsHandle> waitForFunction(
  1. @Language('js') String pageFunction, {
  2. List? args,
  3. Duration? timeout,
  4. Polling? polling,
})

Parameters:

  • pageFunction: Function to be evaluated in browser context
  • polling: An interval at which the pageFunction is executed, defaults to everyFrame.
    • Polling.everyFrame: to constantly execute pageFunction in requestAnimationFrame callback. This is the tightest polling mode which is suitable to observe styling changes.
    • Polling.mutation: to execute pageFunction on every DOM mutation.
    • Polling.interval: An interval at which the function would be executed
  • args: Arguments to pass to pageFunction

Returns a Future which resolves when the pageFunction returns a truthy value. It resolves to a JSHandle of the truthy value.

The waitForFunction can be used to observe viewport size change:

import 'package:puppeteer/puppeteer.dart';

void main() async {
  var browser = await puppeteer.launch();
  var page = await browser.newPage();
  var watchDog = page.mainFrame.waitForFunction('window.innerWidth < 100');
  await page.setViewport(DeviceViewport(width: 50, height: 50));
  await watchDog;
  await browser.close();
}

To pass arguments from node.js to the predicate of page.waitForFunction function:

var selector = '.foo';
await page.mainFrame.waitForFunction(
    'selector => !!document.querySelector(selector)',
    args: [selector]);

Implementation

Future<JsHandle> waitForFunction(@Language('js') String pageFunction,
    {List<dynamic>? args, Duration? timeout, Polling? polling}) {
  return _mainWorld.waitForFunction(pageFunction, args,
      timeout: timeout, polling: polling);
}