waitForFunction method
Parameters:
pageFunction
: Function to be evaluated in browser contextpolling
: An interval at which thepageFunction
is executed, defaults toeveryFrame
.- Polling.everyFrame: to constantly execute
pageFunction
inrequestAnimationFrame
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
- Polling.everyFrame: to constantly execute
args
: Arguments to pass topageFunction
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.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.waitForFunction(
'selector => !!document.querySelector(selector)',
args: [selector],
);
Shortcut for [page.mainFrame().waitForFunction(pageFunction[, options, ...args
])](#framewaitforfunctionpagefunction-options-args).
Implementation
Future<JsHandle> waitForFunction(
@Language('js') String pageFunction, {
List<dynamic>? args,
Duration? timeout,
Polling? polling,
}) {
return mainFrame.waitForFunction(
pageFunction,
args: args,
timeout: timeout,
polling: polling,
);
}