2022-07-05 13:41:43 +00:00
---
sidebar_label: ExecutionContext.evaluateHandle
---
# ExecutionContext.evaluateHandle() method
2022-08-11 09:45:35 +00:00
Evaluates the given function.
Unlike [evaluate ](./puppeteer.executioncontext.evaluate.md ), 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.
2022-07-05 13:41:43 +00:00
**Signature:**
```typescript
class ExecutionContext {
evaluateHandle<
Params extends unknown[],
Func extends EvaluateFunc< Params > = EvaluateFunc< Params >
>(
pageFunction: Func | string,
...args: Params
): Promise< HandleFor < Awaited < ReturnType < Func > >>>;
}
```
## Parameters
2022-08-11 09:45:35 +00:00
| Parameter | Type | Description |
| ------------ | -------------- | ----------------------------------------------- |
| pageFunction | Func \| string | The function to evaluate. |
| args | Params | Additional arguments to pass into the function. |
2022-07-05 13:41:43 +00:00
**Returns:**
Promise< [HandleFor](./puppeteer.handlefor.md)< Awaited< ReturnType< Func> > > >
2022-08-11 09:45:35 +00:00
A [handle ](./puppeteer.jshandle.md ) to the result of evaluating the function. If the result is a `Node` , then this will return an [element handle ](./puppeteer.elementhandle.md ).
2022-07-05 13:41:43 +00:00
## Example 1
```ts
const context = await page.mainFrame().executionContext();
2022-08-11 09:45:35 +00:00
const handle: JSHandle< typeof globalThis > = await context.evaluateHandle(() =>
Promise.resolve(self)
);
2022-07-05 13:41:43 +00:00
```
## Example 2
A string can also be passed in instead of a function.
```ts
2022-08-11 09:45:35 +00:00
const handle: JSHandle< number > = await context.evaluateHandle('1 + 2');
2022-07-05 13:41:43 +00:00
```
## Example 3
2022-08-11 09:45:35 +00:00
Handles can also be passed as `args` . They resolve to their referenced object:
2022-07-05 13:41:43 +00:00
```ts
2022-08-11 09:45:35 +00:00
const bodyHandle: ElementHandle< HTMLBodyElement > = 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();
2022-07-05 13:41:43 +00:00
```