Skip to main content
Module

x/puppeteer/mod.ts>ExecutionContext#evaluateHandle

A port of puppeteer running on Deno
Latest
method ExecutionContext.prototype.evaluateHandle
Re-export
import { ExecutionContext } from "https://deno.land/x/puppeteer@16.2.0/mod.ts";

Evaluates the given function.

Unlike ExecutionContext.evaluate | evaluate, this method returns a handle to the result of the function.

This method may be better suited if the object cannot be serialized (e.g. Map) and requires further manipulation.

Examples

Example 1

const context = await page.mainFrame().executionContext();
const handle: JSHandle<typeof globalThis> = await context.evaluateHandle(
  () => Promise.resolve(self)
);

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

const handle: JSHandle<number> = await context.evaluateHandle('1 + 2');

Handles can also be passed as args. They resolve to their referenced object:

const bodyHandle: ElementHandle<HTMLBodyany> =
  await context.evaluateHandle(() => {
    return document.body;
  });
const stringHandle: JSHandle<string> = await context.evaluateHandle(
  body => body.innerHTML,
  body
);
console.log(await stringHandle.jsonValue()); // prints body's innerHTML
// Always dispose your garbage! :)
await bodyHandle.dispose();
await stringHandle.dispose();

Type Parameters

Params extends unknown[]
optional
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>

Parameters

pageFunction: Func | string
  • The function to evaluate.
...args: Params
  • Additional arguments to pass into the function.

Returns

Promise<HandleFor<Awaited<ReturnType<Func>>>>

A JSHandle | handle to the result of evaluating the function. If the result is a Node, then this will return an ElementHandle | element handle.