Skip to main content

ElementHandle

ElementHandle represents an in-page DOM element. ElementHandles can be created with the Page.querySelector() method.

Discouraged

The use of ElementHandle is discouraged, use Locator objects and web-first assertions instead.

ElementHandle hrefElement = page.querySelector("a");
hrefElement.click();

ElementHandle prevents DOM element from garbage collection unless the handle is disposed with JSHandle.dispose(). ElementHandles are auto-disposed when their origin frame gets navigated.

ElementHandle instances can be used as an argument in Page.evalOnSelector() and Page.evaluate() methods.

The difference between the Locator and ElementHandle is that the ElementHandle points to a particular element, while Locator captures the logic of how to retrieve an element.

In the example below, handle points to a particular DOM element on page. If that element changes text or is used by React to render an entirely different component, handle is still pointing to that very DOM element. This can lead to unexpected behaviors.

ElementHandle handle = page.querySelector("text=Submit");
handle.hover();
handle.click();

With the locator, every time the element is used, up-to-date DOM element is located in the page using the selector. So in the snippet below, underlying DOM element is going to be located twice.

Locator locator = page.getByText("Submit");
locator.hover();
locator.click();

Methods​

boundingBox​

Added before v1.9 elementHandle.boundingBox

This method returns the bounding box of the element, or null if the element is not visible. The bounding box is calculated relative to the main frame viewport - which is usually the same as the browser window.

Scrolling affects the returned bounding box, similarly to Element.getBoundingClientRect. That means x and/or y may be negative.

Elements from child frames return the bounding box relative to the main frame, unlike the Element.getBoundingClientRect.

Assuming the page is static, it is safe to use bounding box coordinates to perform input. For example, the following snippet should click the center of the element.

Usage

BoundingBox box = elementHandle.boundingBox();
page.mouse().click(box.x + box.width / 2, box.y + box.height / 2);

Returns

  • null | BoundingBox#
    • x double

      the x coordinate of the element in pixels.

    • y double

      the y coordinate of the element in pixels.

    • width double

      the width of the element in pixels.

    • height double

      the height of the element in pixels.


contentFrame​

Added before v1.9 elementHandle.contentFrame

Returns the content frame for element handles referencing iframe nodes, or null otherwise

Usage

ElementHandle.contentFrame();

Returns


ownerFrame​

Added before v1.9 elementHandle.ownerFrame

Returns the frame containing the given element.

Usage

ElementHandle.ownerFrame();

Returns


waitForElementState​

Added before v1.9 elementHandle.waitForElementState

Returns when the element satisfies the state.

Depending on the state parameter, this method waits for one of the actionability checks to pass. This method throws when the element is detached while waiting, unless waiting for the "hidden" state.

  • "visible" Wait until the element is visible.
  • "hidden" Wait until the element is not visible or not attached. Note that waiting for hidden does not throw when the element detaches.
  • "stable" Wait until the element is both visible and stable.
  • "enabled" Wait until the element is enabled.
  • "disabled" Wait until the element is not enabled.
  • "editable" Wait until the element is editable.

If the element does not satisfy the condition for the setTimeout milliseconds, this method will throw.

Usage

ElementHandle.waitForElementState(state);
ElementHandle.waitForElementState(state, options);

Arguments

  • state enum ElementState { VISIBLE, HIDDEN, STABLE, ENABLED, DISABLED, EDITABLE }#

    A state to wait for, see below for more details.

  • options ElementHandle.WaitForElementStateOptions (optional)

Returns


Deprecated​

check​

Added before v1.9 elementHandle.check
Discouraged

Use locator-based Locator.check() instead. Read more about locators.

This method checks the element by performing the following steps:

  1. Ensure that element is a checkbox or a radio input. If not, this method throws. If the element is already checked, this method returns immediately.
  2. Wait for actionability checks on the element, unless setForce option is set.
  3. Scroll the element into view if needed.
  4. Use Page.mouse() to click in the center of the element.
  5. Ensure that the element is now checked. If not, this method throws.

If the element is detached from the DOM at any moment during the action, this method throws.

When all steps combined have not finished during the specified setTimeout, this method throws a TimeoutError. Passing zero timeout disables this.

Usage

ElementHandle.check();
ElementHandle.check(options);

Arguments

  • options ElementHandle.CheckOptions (optional)
    • setForce boolean (optional)#

      Whether to bypass the actionability checks. Defaults to false.

    • setNoWaitAfter boolean (optional)#

      Deprecated

      This option has no effect.

      This option has no effect.

    • setPosition Position (optional) Added in: v1.11#

      A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.

    • setScroll enum ScrollMode { AUTO, NONE } (optional) Added in: v1.62#

      Controls whether Playwright scrolls the element into view before performing the action. Defaults to "auto", which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to "none", Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

    • setTimeout double (optional)#

      Maximum time in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the BrowserContext.setDefaultTimeout() or Page.setDefaultTimeout() methods.

    • setTrial boolean (optional) Added in: v1.11#

      When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it.

Returns


click​

Added before v1.9 elementHandle.click
Discouraged

Use locator-based Locator.click() instead. Read more about locators.

This method clicks the element by performing the following steps:

  1. Wait for actionability checks on the element, unless setForce option is set.
  2. Scroll the element into view if needed.
  3. Use Page.mouse() to click in the center of the element, or the specified setPosition.
  4. Wait for initiated navigations to either succeed or fail, unless setNoWaitAfter option is set.

If the element is detached from the DOM at any moment during the action, this method throws.

When all steps combined have not finished during the specified setTimeout, this method throws a TimeoutError. Passing zero timeout disables this.

Usage

ElementHandle.click();