evaluateHandle<T extends JsHandle> method

Future<T> evaluateHandle<T extends JsHandle>(
  1. @Language.new('js') String pageFunction, {
  2. List? args,
})

The only difference between executionContext.evaluate and executionContext.evaluateHandle is that executionContext.evaluateHandle returns in-page object (JSHandle).

If the function passed to the executionContext.evaluateHandle returns a Promise, then executionContext.evaluateHandle would wait for the promise to resolve and return its value.

var context = await page.mainFrame.executionContext;
var aHandle = await context.evaluateHandle('() => Promise.resolve(self)');
print(aHandle); // Handle for the global object.

A string can also be passed in instead of a function.

var aHandle = await context.evaluateHandle(
  '1 + 2',
); // Handle for the '3' object.

JSHandle instances can be passed as arguments to the executionContext.evaluateHandle:

var aHandle = await context.evaluateHandle('() => document.body');
var resultHandle = await context.evaluateHandle(
  'body => body.innerHTML',
  args: [aHandle],
);
print(await resultHandle.jsonValue); // prints body's innerHTML
await aHandle.dispose();
await resultHandle.dispose();

Implementation

Future<T> evaluateHandle<T extends JsHandle>(
  @Language('js') String pageFunction, {
  List<dynamic>? args,
}) async =>
    await _evaluateInternal(pageFunction, args: args, returnByValue: false);