mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
89 lines
18 KiB
Markdown
89 lines
18 KiB
Markdown
---
|
|
sidebar_label: ElementHandle
|
|
---
|
|
|
|
# ElementHandle class
|
|
|
|
ElementHandle represents an in-page DOM element.
|
|
|
|
#### Signature:
|
|
|
|
```typescript
|
|
export declare abstract class ElementHandle<ElementType extends Node = Element> extends JSHandle<ElementType>
|
|
```
|
|
|
|
**Extends:** [JSHandle](./puppeteer.jshandle.md)<ElementType>
|
|
|
|
## Remarks
|
|
|
|
ElementHandles can be created with the [Page.$()](./puppeteer.page._.md) method.
|
|
|
|
```ts
|
|
import puppeteer from 'puppeteer';
|
|
|
|
(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.
|
|
|
|
ElementHandle instances can be used as arguments in [Page.$eval()](./puppeteer.page._eval.md) and [Page.evaluate()](./puppeteer.page.evaluate.md) methods.
|
|
|
|
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.
|
|
|
|
## Properties
|
|
|
|
| Property | Modifiers | Type | Description |
|
|
| -------- | --------------------- | ----------------------------- | ------------------------------------------ |
|
|
| frame | <code>readonly</code> | [Frame](./puppeteer.frame.md) | Frame corresponding to the current handle. |
|
|
|
|
## Methods
|
|
|
|
| Method | Modifiers | Description |
|
|
| ----------------------------------------------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| [$](./puppeteer.elementhandle._.md) | | Queries the current element for an element matching the given selector. |
|
|
| [$$](./puppeteer.elementhandle.__.md) | | Queries the current element for all elements matching the given selector. |
|
|
| [$$eval](./puppeteer.elementhandle.__eval.md) | | <p>Runs the given function on an array of elements matching the given selector in the current element.</p><p>If the given function returns a promise, then this method will wait till the promise resolves.</p> |
|
|
| [$eval](./puppeteer.elementhandle._eval.md) | | <p>Runs the given function on the first element matching the given selector in the current element.</p><p>If the given function returns a promise, then this method will wait till the promise resolves.</p> |
|
|
| [$x](./puppeteer.elementhandle._x.md) | | |
|
|
| [autofill](./puppeteer.elementhandle.autofill.md) | | 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. |
|
|
| [boundingBox](./puppeteer.elementhandle.boundingbox.md) | | This method returns the bounding box of the element (relative to the main frame), or <code>null</code> if the element is [not part of the layout](https://drafts.csswg.org/css-display-4/#box-generation) (example: <code>display: none</code>). |
|
|
| [boxModel](./puppeteer.elementhandle.boxmodel.md) | | This method returns boxes of the element, or <code>null</code> if the element is [not part of the layout](https://drafts.csswg.org/css-display-4/#box-generation) (example: <code>display: none</code>). |
|
|
| [click](./puppeteer.elementhandle.click.md) | | This method scrolls element into view if needed, and then uses [Page.mouse](./puppeteer.page.md) to click in the center of the element. If the element is detached from DOM, the method throws an error. |
|
|
| [clickablePoint](./puppeteer.elementhandle.clickablepoint.md) | | Returns the middle point within an element unless a specific offset is provided. |
|
|
| [contentFrame](./puppeteer.elementhandle.contentframe.md) | | Resolves the frame associated with the element, if any. Always exists for HTMLIFrameElements. |
|
|
| [contentFrame](./puppeteer.elementhandle.contentframe_1.md) | | |
|
|
| [drag](./puppeteer.elementhandle.drag.md) | | Drags an element over the given element or point. |
|
|
| [dragAndDrop](./puppeteer.elementhandle.draganddrop.md) | | |
|
|
| [dragEnter](./puppeteer.elementhandle.dragenter.md) | | |
|
|
| [dragOver](./puppeteer.elementhandle.dragover.md) | | |
|
|
| [drop](./puppeteer.elementhandle.drop.md) | | Drops the given element onto the current one. |
|
|
| [drop](./puppeteer.elementhandle.drop_1.md) | | |
|
|
| [focus](./puppeteer.elementhandle.focus.md) | | Calls [focus](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on the element. |
|
|
| [hover](./puppeteer.elementhandle.hover.md) | | This method scrolls element into view if needed, and then uses [Page](./puppeteer.page.md) to hover over the center of the element. If the element is detached from DOM, the method throws an error. |
|
|
| [isHidden](./puppeteer.elementhandle.ishidden.md) | | Checks if an element is hidden using the same mechanism as [ElementHandle.waitForSelector()](./puppeteer.elementhandle.waitforselector.md). |
|
|
| [isIntersectingViewport](./puppeteer.elementhandle.isintersectingviewport.md) | | 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. |
|
|
| [isVisible](./puppeteer.elementhandle.isvisible.md) | | Checks if an element is visible using the same mechanism as [ElementHandle.waitForSelector()](./puppeteer.elementhandle.waitforselector.md). |
|
|
| [press](./puppeteer.elementhandle.press.md) | | Focuses the element, and then uses [Keyboard.down()](./puppeteer.keyboard.down.md) and [Keyboard.up()](./puppeteer.keyboard.up.md). |
|
|
| [screenshot](./puppeteer.elementhandle.screenshot.md) | | 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. |
|
|
| [screenshot](./puppeteer.elementhandle.screenshot_1.md) | | |
|
|
| [scrollIntoView](./puppeteer.elementhandle.scrollintoview.md) | | Scrolls the element into view using either the automation protocol client or by calling element.scrollIntoView. |
|
|
| [select](./puppeteer.elementhandle.select.md) | | Triggers a <code>change</code> and <code>input</code> event once all the provided options have been selected. If there's no <code><select></code> element matching <code>selector</code>, the method throws an error. |
|
|
| [tap](./puppeteer.elementhandle.tap.md) | | 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. |
|
|
| [toElement](./puppeteer.elementhandle.toelement.md) | | Converts the current handle to the given element type. |
|
|
| [touchEnd](./puppeteer.elementhandle.touchend.md) | | |
|
|
| [touchMove](./puppeteer.elementhandle.touchmove.md) | | |
|
|
| [touchStart](./puppeteer.elementhandle.touchstart.md) | | |
|
|
| [type](./puppeteer.elementhandle.type.md) | | <p>Focuses the element, and then sends a <code>keydown</code>, <code>keypress</code>/<code>input</code>, and <code>keyup</code> event for each character in the text.</p><p>To press a special key, like <code>Control</code> or <code>ArrowDown</code>, use [ElementHandle.press()](./puppeteer.elementhandle.press.md).</p> |
|
|
| [uploadFile](./puppeteer.elementhandle.uploadfile.md) | | Sets the value of an [input element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input) to the given file paths. |
|
|
| [waitForSelector](./puppeteer.elementhandle.waitforselector.md) | | <p>Wait for an element matching the given selector to appear in the current element.</p><p>Unlike [Frame.waitForSelector()](./puppeteer.frame.waitforselector.md), this method does not work across navigations or if the element is detached from DOM.</p> |
|
|
| [waitForXPath](./puppeteer.elementhandle.waitforxpath.md) | | |
|