$eval<T> method

Future<T?> $eval<T>(
  1. String selector,
  2. @Language('js') String pageFunction, {
  3. List? args,
})

This method runs document.querySelector within the element and passes it as the first argument to pageFunction. If there's no element matching selector, the method throws an error.

If pageFunction returns a Promise, then frame.$eval would wait for the promise to resolve and return its value.

Examples:

var tweetHandle = await page.$('.tweet');
expect(await tweetHandle.$eval('.like', 'node => node.innerText'), '100');
expect(await tweetHandle.$eval('.retweets', 'node => node.innerText'), '10');

Parameters:

  • A selector to query page for
  • pageFunction: Function to be evaluated in browser context
  • args: Arguments to pass to pageFunction

Returns Future which resolves to the return value of pageFunction.

Implementation

Future<T?> $eval<T>(String selector, @Language('js') String pageFunction,
    {List<dynamic>? args}) async {
  var elementHandle = await $OrNull(selector);
  if (elementHandle == null) {
    throw Exception(
        'Error: failed to find element matching selector "$selector"');
  }

  var result = await elementHandle.evaluate<T>(pageFunction, args: args);
  await elementHandle.dispose();
  return result;
}