2022-07-05 13:41:43 +00:00
---
sidebar_label: ElementHandle
---
# ElementHandle class
ElementHandle represents an in-page DOM element.
2022-10-24 07:07:05 +00:00
#### Signature:
2022-07-05 13:41:43 +00:00
```typescript
2023-08-23 16:00:34 +00:00
export declare abstract class ElementHandle< ElementType extends Node = Element > extends JSHandle< ElementType >
2022-07-05 13:41:43 +00:00
```
2022-08-11 05:55:02 +00:00
**Extends:** [JSHandle ](./puppeteer.jshandle.md )< ElementType>
2022-07-05 13:41:43 +00:00
## Remarks
2022-07-06 07:05:37 +00:00
ElementHandles can be created with the [Page.$() ](./puppeteer.page._.md ) method.
2022-07-05 13:41:43 +00:00
```ts
2022-12-09 12:57:39 +00:00
import puppeteer from 'puppeteer';
2022-07-05 13:41:43 +00:00
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const hrefElement = await page.$('a');
await hrefElement.click();
// ...
})();
```
ElementHandle prevents the DOM element from being garbage-collected unless the handle is [disposed ](./puppeteer.jshandle.dispose.md ). ElementHandles are auto-disposed when their origin frame gets navigated.
2022-07-06 07:05:37 +00:00
ElementHandle instances can be used as arguments in [Page.$eval() ](./puppeteer.page._eval.md ) and [Page.evaluate() ](./puppeteer.page.evaluate.md ) methods.
2022-07-05 13:41:43 +00:00
If you're using TypeScript, ElementHandle takes a generic argument that denotes the type of element the handle is holding within. For example, if you have a handle to a `<select>` element, you can type it as `ElementHandle<HTMLSelectElement>` and you get some nicer type checks.
The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `ElementHandle` class.
2022-08-31 19:57:02 +00:00
## Properties
2024-03-20 15:03:14 +00:00
< table > < thead > < tr > < th >
Property
< / th > < th >
Modifiers
< / th > < th >
Type
< / th > < th >
Description
< / th > < / tr > < / thead >
< tbody > < tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "frame" > frame< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
`readonly`
< / td > < td >
[Frame ](./puppeteer.frame.md )
< / td > < td >
Frame corresponding to the current handle.
< / td > < / tr >
< / tbody > < / table >
2022-08-31 19:57:02 +00:00
2022-07-05 13:41:43 +00:00
## Methods
2024-03-20 15:03:14 +00:00
< table > < thead > < tr > < th >
Method
< / th > < th >
Modifiers
< / th > < th >
Description
< / th > < / tr > < / thead >
< tbody > < tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "_" > [$(selector)](./puppeteer.elementhandle._.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Queries the current element for an element matching the given selector.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "__" > [$$(selector)](./puppeteer.elementhandle.__.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Queries the current element for all elements matching the given selector.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "__eval" > [$$eval(selector, pageFunction, args)](./puppeteer.elementhandle.__eval.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Runs the given function on an array of elements matching the given selector in the current element.
If the given function returns a promise, then this method will wait till the promise resolves.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "_eval" > [$eval(selector, pageFunction, args)](./puppeteer.elementhandle._eval.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Runs the given function on the first element matching the given selector in the current element.
If the given function returns a promise, then this method will wait till the promise resolves.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "autofill" > [autofill(data)](./puppeteer.elementhandle.autofill.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
If the element is a form input, you can use [ElementHandle.autofill() ](./puppeteer.elementhandle.autofill.md ) to test if the form is compatible with the browser's autofill implementation. Throws an error if the form cannot be autofilled.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "boundingbox" > [boundingBox()](./puppeteer.elementhandle.boundingbox.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
This method returns the bounding box of the element (relative to the main frame), or `null` if the element is [not part of the layout ](https://drafts.csswg.org/css-display-4/#box-generation ) (example: `display: none` ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "boxmodel" > [boxModel()](./puppeteer.elementhandle.boxmodel.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
This method returns boxes of the element, or `null` if the element is [not part of the layout ](https://drafts.csswg.org/css-display-4/#box-generation ) (example: `display: none` ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "click" > [click(this, options)](./puppeteer.elementhandle.click.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
2024-04-11 11:01:13 +00:00
This method scrolls element into view if needed, and then uses [Page.mouse ](./puppeteer.page.md#mouse ) to click in the center of the element. If the element is detached from DOM, the method throws an error.
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "clickablepoint" > [clickablePoint(offset)](./puppeteer.elementhandle.clickablepoint.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Returns the middle point within an element unless a specific offset is provided.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "contentframe" > [contentFrame(this)](./puppeteer.elementhandle.contentframe.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Resolves the frame associated with the element, if any. Always exists for HTMLIFrameElements.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "contentframe" > [contentFrame()](./puppeteer.elementhandle.contentframe_1.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "drag" > [drag(this, target)](./puppeteer.elementhandle.drag.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Drags an element over the given element or point.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "draganddrop" > [dragAndDrop(this, target, options)](./puppeteer.elementhandle.draganddrop.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
`deprecated`
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
**Deprecated:**
Use `ElementHandle.drop` instead.
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "dragenter" > [dragEnter(this, data)](./puppeteer.elementhandle.dragenter.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
`deprecated`
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
**Deprecated:**
Do not use. `dragenter` will automatically be performed during dragging.
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "dragover" > [dragOver(this, data)](./puppeteer.elementhandle.dragover.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
`deprecated`
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
**Deprecated:**
Do not use. `dragover` will automatically be performed during dragging.
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "drop" > [drop(this, element)](./puppeteer.elementhandle.drop.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Drops the given element onto the current one.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "drop" > [drop(this, data)](./puppeteer.elementhandle.drop_1.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
`deprecated`
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
**Deprecated:**
No longer supported.
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "focus" > [focus()](./puppeteer.elementhandle.focus.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Calls [focus ](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus ) on the element.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "hover" > [hover(this)](./puppeteer.elementhandle.hover.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
2024-04-11 11:01:13 +00:00
This method scrolls element into view if needed, and then uses [Page.mouse ](./puppeteer.page.md#mouse ) to hover over the center of the element. If the element is detached from DOM, the method throws an error.
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "ishidden" > [isHidden()](./puppeteer.elementhandle.ishidden.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Checks if an element is hidden using the same mechanism as [ElementHandle.waitForSelector() ](./puppeteer.elementhandle.waitforselector.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "isintersectingviewport" > [isIntersectingViewport(this, options)](./puppeteer.elementhandle.isintersectingviewport.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Resolves to true if the element is visible in the current viewport. If an element is an SVG, we check if the svg owner element is in the viewport instead. See https://crbug.com/963246.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "isvisible" > [isVisible()](./puppeteer.elementhandle.isvisible.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Checks if an element is visible using the same mechanism as [ElementHandle.waitForSelector() ](./puppeteer.elementhandle.waitforselector.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "press" > [press(key, options)](./puppeteer.elementhandle.press.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Focuses the element, and then uses [Keyboard.down() ](./puppeteer.keyboard.down.md ) and [Keyboard.up() ](./puppeteer.keyboard.up.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "screenshot" > [screenshot(options)](./puppeteer.elementhandle.screenshot.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
This method scrolls element into view if needed, and then uses [Page.screenshot() ](./puppeteer.page.screenshot_1.md ) to take a screenshot of the element. If the element is detached from DOM, the method throws an error.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "screenshot" > [screenshot(options)](./puppeteer.elementhandle.screenshot_1.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "scrollintoview" > [scrollIntoView(this)](./puppeteer.elementhandle.scrollintoview.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Scrolls the element into view using either the automation protocol client or by calling element.scrollIntoView.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "select" > [select(values)](./puppeteer.elementhandle.select.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Triggers a `change` and `input` event once all the provided options have been selected. If there's no `<select>` element matching `selector` , the method throws an error.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "tap" > [tap(this)](./puppeteer.elementhandle.tap.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
This method scrolls element into view if needed, and then uses [Touchscreen.tap() ](./puppeteer.touchscreen.tap.md ) to tap in the center of the element. If the element is detached from DOM, the method throws an error.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "toelement" > [toElement(tagName)](./puppeteer.elementhandle.toelement.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Converts the current handle to the given element type.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "touchend" > [touchEnd(this)](./puppeteer.elementhandle.touchend.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "touchmove" > [touchMove(this)](./puppeteer.elementhandle.touchmove.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "touchstart" > [touchStart(this)](./puppeteer.elementhandle.touchstart.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "type" > [type(text, options)](./puppeteer.elementhandle.type.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Focuses the element, and then sends a `keydown` , `keypress` /`input`, and `keyup` event for each character in the text.
To press a special key, like `Control` or `ArrowDown` , use [ElementHandle.press() ](./puppeteer.elementhandle.press.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "uploadfile" > [uploadFile(this, paths)](./puppeteer.elementhandle.uploadfile.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Sets the value of an [input element ](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input ) to the given file paths.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "waitforselector" > [waitForSelector(selector, options)](./puppeteer.elementhandle.waitforselector.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Wait for an element matching the given selector to appear in the current element.
Unlike [Frame.waitForSelector() ](./puppeteer.frame.waitforselector.md ), this method does not work across navigations or if the element is detached from DOM.
< / td > < / tr >
< / tbody > < / table >