Skip to main content
Version: 1.13.0

Handles

Playwright can create handles to the page DOM elements or any other objects inside the page. These handles live in the Playwright process, whereas the actual objects live in the browser. There are two types of handles:

  • JSHandle to reference any JavaScript objects in the page
  • ElementHandle to reference DOM elements in the page, it has extra methods that allow performing actions on the elements and asserting their properties.

Since any DOM element in the page is also a JavaScript object, any ElementHandle is a JSHandle as well.

Handles are used to perform operations on those actual objects in the page. You can evaluate on a handle, get handle properties, pass handle as an evaluation parameter, serialize page object into JSON etc. See the JSHandle class API for these and methods.

API reference#

Here is the easiest way to obtain a JSHandle.

var jsHandle = await page.EvaluateHandleAsync("window");//  Use jsHandle for evaluations.
var ulElementHandle = await page.WaitForSelectorAsync("ul");//  Use ulElementHandle for actions and evaluation.

Element Handles#

note

It is recommended to use selector-based actions like Page.ClickAsync(selector, options) rather than using the ElementHandle for input actions, unless your use case specifically requires the use of handles.

When ElementHandle is required, it is recommended to fetch it with the Page.WaitForSelectorAsync(selector, options) or Frame.WaitForSelectorAsync(selector, options) methods. These APIs wait for the element to be attached and visible.

// Get the element handlevar jsHandle = await page.WaitForSelectorAsync("#box");var elementHandle = jsHandle as ElementHandle;
// Assert bounding box for the elementvar boundingBox = await elementHandle.BoundingBoxAsync();Assert.Equal(100, boundingBox.Width);
// Assert attribute for the elementvar classNames = await elementHandle.GetAttributeAsync("class");Assert.True(classNames.Contains("highlighted"));

Handles as parameters#

Handles can be passed into the Page.EvaluateAsync(expression, arg) and similar methods. The following snippet creates a new array in the page, initializes it with data and returns a handle to this array into Playwright. It then uses the handle in subsequent evaluations:

// Create new array in page.var myArrayHandle = await page.EvaluateHandleAsync(@"() => {    window.myArray = [1];    return myArray;}");
// Get the length of the array.var length = await page.EvaluateAsync<int>("a => a.length", myArrayHandle);
// Add one more element to the array using the handleawait page.EvaluateAsync("arg => arg.myArray.add(arg.newElement)",    new { myArray = myArrayHandle, newElement = 2 });
// Release the object when it is no longer needed.await myArrayHandle.DisposeAsync();

Handle Lifecycle#

Handles can be acquired using the page methods such as Page.EvaluateHandleAsync(expression, arg), Page.QuerySelectorAsync(selector) or Page.QuerySelectorAllAsync(selector) or their frame counterparts Frame.EvaluateHandleAsync(expression, arg), Frame.QuerySelectorAsync(selector) or Frame.QuerySelectorAllAsync(selector). Once created, handles will retain object from garbage collection unless page navigates or the handle is manually disposed via the JsHandle.DisposeAsync() method.

API reference#