# class: ElementHandle * extends: [JSHandle] ElementHandle represents an in-page DOM element. ElementHandles can be created with the [`method: Page.querySelector`] method. ```js const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'. (async () => { const browser = await chromium.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); const hrefElement = await page.$('a'); await hrefElement.click(); // ... })(); ``` ```java import com.microsoft.playwright.*; public class Example { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { BrowserType chromium = playwright.chromium(); Browser browser = chromium.launch(); Page page = browser.newPage(); page.navigate("https://example.com"); ElementHandle hrefElement = page.querySelector("a"); hrefElement.click(); // ... } } } ``` ```python async import asyncio from playwright.async_api import async_playwright async def run(playwright): chromium = playwright.chromium browser = await chromium.launch() page = await browser.new_page() await page.goto("https://example.com") href_element = await page.query_selector("a") await href_element.click() # ... async def main(): async with async_playwright() as playwright: await run(playwright) asyncio.run(main()) ``` ```python sync from playwright.sync_api import sync_playwright def run(playwright): chromium = playwright.chromium browser = chromium.launch() page = browser.new_page() page.goto("https://example.com") href_element = page.query_selector("a") href_element.click() # ... with sync_playwright() as playwright: run(playwright) ``` ElementHandle prevents DOM element from garbage collection unless the handle is disposed with [`method: JSHandle.dispose`]. ElementHandles are auto-disposed when their origin frame gets navigated. ElementHandle instances can be used as an argument in [`method: Page.evalOnSelector`] and [`method: Page.evaluate`] methods. ## async method: ElementHandle.boundingBox - returns: <[null]|[Object]> - `x` <[float]> the x coordinate of the element in pixels. - `y` <[float]> the y coordinate of the element in pixels. - `width` <[float]> the width of the element in pixels. - `height` <[float]> the height of the element in pixels. 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 bonding box, similarly to [Element.getBoundingClientRect](https://developer.mozilla.org/en-US/docs/Web/API/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](https://developer.mozilla.org/en-US/docs/Web/API/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. ```js const box = await elementHandle.boundingBox(); await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2); ``` ```java BoundingBox box = elementHandle.boundingBox(); page.mouse().click(box.x + box.width / 2, box.y + box.height / 2); ``` ```python async box = await element_handle.bounding_box() await page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2) ``` ```python sync box = element_handle.bounding_box() page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2) ``` ## async method: ElementHandle.check 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. 1. Wait for [actionability](./actionability.md) checks on the element, unless [`option: force`] option is set. 1. Scroll the element into view if needed. 1. Use [`property: Page.mouse`] to click in the center of the element. 1. Wait for initiated navigations to either succeed or fail, unless [`option: noWaitAfter`] option is set. 1. 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 [`option: timeout`], this method throws a [TimeoutError]. Passing zero timeout disables this. ### option: ElementHandle.check.force = %%-input-force-%% ### option: ElementHandle.check.noWaitAfter = %%-input-no-wait-after-%% ### option: ElementHandle.check.position = %%-input-position-%% ### option: ElementHandle.check.timeout = %%-input-timeout-%% ### option: ElementHandle.check.trial = %%-input-trial-%% ## async method: ElementHandle.click This method clicks the element by performing the following steps: 1. Wait for [actionability](./actionability.md) checks on the element, unless [`option: force`] option is set. 1. Scroll the element into view if needed. 1. Use [`property: Page.mouse`] to click in the center of the element, or the specified [`option: position`]. 1. Wait for initiated navigations to either succeed or fail, unless [`option: noWaitAfter`] 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 [`option: timeout`], this method throws a [TimeoutError]. Passing zero timeout disables this. ### option: ElementHandle.click.button = %%-input-button-%% ### option: ElementHandle.click.clickCount = %%-input-click-count-%% ### option: ElementHandle.click.delay = %%-input-down-up-delay-%% ### option: ElementHandle.click.position = %%-input-position-%% ### option: ElementHandle.click.modifiers = %%-input-modifiers-%% ### option: ElementHandle.click.force = %%-input-force-%% ### option: ElementHandle.click.noWaitAfter = %%-input-no-wait-after-%% ### option: ElementHandle.click.timeout = %%-input-timeout-%% ### option: ElementHandle.click.trial = %%-input-trial-%% ## async method: ElementHandle.contentFrame - returns: <[null]|[Frame]> Returns the content frame for element handles referencing iframe nodes, or `null` otherwise ## async method: ElementHandle.dblclick * langs: - alias-csharp: DblClickAsync This method double clicks the element by performing the following steps: 1. Wait for [actionability](./actionability.md) checks on the element, unless [`option: force`] option is set. 1. Scroll the element into view if needed. 1. Use [`property: Page.mouse`] to double click in the center of the element, or the specified [`option: position`]. 1. Wait for initiated navigations to either succeed or fail, unless [`option: noWaitAfter`] option is set. Note that if the first click of the `dblclick()` triggers a navigation event, this method will throw. 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 [`option: timeout`], this method throws a [TimeoutError]. Passing zero timeout disables this. :::note `elementHandle.dblclick()` dispatches two `click` events and a single `dblclick` event. ::: ### option: ElementHandle.dblclick.button = %%-input-button-%% ### option: ElementHandle.dblclick.delay = %%-input-down-up-delay-%% ### option: ElementHandle.dblclick.position = %%-input-position-%% ### option: ElementHandle.dblclick.modifiers = %%-input-modifiers-%% ### option: ElementHandle.dblclick.force = %%-input-force-%% ### option: ElementHandle.dblclick.noWaitAfter = %%-input-no-wait-after-%% ### option: ElementHandle.dblclick.timeout = %%-input-timeout-%% ### option: ElementHandle.dblclick.trial = %%-input-trial-%% ## async method: ElementHandle.dispatchEvent The snippet below dispatches the `click` event on the element. Regardless of the visibility state of the element, `click` is dispatched. This is equivalent to calling [element.click()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click). ```js await elementHandle.dispatchEvent('click'); ``` ```java elementHandle.dispatchEvent("click"); ``` ```python async await element_handle.dispatch_event("click") ``` ```python sync element_handle.dispatch_event("click") ``` Under the hood, it creates an instance of an event based on the given [`param: type`], initializes it with [`param: eventInit`] properties and dispatches it on the element. Events are `composed`, `cancelable` and bubble by default. Since [`param: eventInit`] is event-specific, please refer to the events documentation for the lists of initial properties: * [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent/DragEvent) * [FocusEvent](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/FocusEvent) * [KeyboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent) * [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent) * [PointerEvent](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent) * [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent) * [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event) You can also specify `JSHandle` as the property value if you want live objects to be passed into the event: ```js // Note you can only create DataTransfer in Chromium and Firefox const dataTransfer = await page.evaluateHandle(() => new DataTransfer()); await elementHandle.dispatchEvent('dragstart', { dataTransfer }); ``` ```java // Note you can only create DataTransfer in Chromium and Firefox JSHandle dataTransfer = page.evaluateHandle("() => new DataTransfer()"); Map arg = new HashMap<>(); arg.put("dataTransfer", dataTransfer); elementHandle.dispatchEvent("dragstart", arg); ``` ```python async # note you can only create data_transfer in chromium and firefox data_transfer = await page.evaluate_handle("new DataTransfer()") await element_handle.dispatch_event("#source", "dragstart", {"dataTransfer": data_transfer}) ``` ```python sync # note you can only create data_transfer in chromium and firefox data_transfer = page.evaluate_handle("new DataTransfer()") element_handle.dispatch_event("#source", "dragstart", {"dataTransfer": data_transfer}) ``` ### param: ElementHandle.dispatchEvent.type - `type` <[string]> DOM event type: `"click"`, `"dragstart"`, etc. ### param: ElementHandle.dispatchEvent.eventInit - `eventInit` <[EvaluationArgument]> Optional event-specific initialization properties. ## async method: ElementHandle.evalOnSelector * langs: - alias-python: eval_on_selector - alias-js: $eval - returns: <[Serializable]> Returns the return value of [`param: expression`]. The method finds an element matching the specified selector in the `ElementHandle`s subtree and passes it as a first argument to [`param: expression`]. See [Working with selectors](./selectors.md) for more details. If no elements match the selector, the method throws an error. If [`param: expression`] returns a [Promise], then [`method: ElementHandle.evalOnSelector`] would wait for the promise to resolve and return its value. Examples: ```js const tweetHandle = await page.$('.tweet'); expect(await tweetHandle.$eval('.like', node => node.innerText)).toBe('100'); expect(await tweetHandle.$eval('.retweets', node => node.innerText)).toBe('10'); ``` ```java ElementHandle tweetHandle = page.querySelector(".tweet"); assertEquals("100", tweetHandle.evalOnSelector(".like", "node => node.innerText")); assertEquals("10", tweetHandle.evalOnSelector(".retweets", "node => node.innerText")); ``` ```python async tweet_handle = await page.query_selector(".tweet") assert await tweet_handle.eval_on_selector(".like", "node => node.innerText") == "100" assert await tweet_handle.eval_on_selector(".retweets", "node => node.innerText") = "10" ``` ```python sync tweet_handle = page.query_selector(".tweet") assert tweet_handle.eval_on_selector(".like", "node => node.innerText") == "100" assert tweet_handle.eval_on_selector(".retweets", "node => node.innerText") = "10" ``` ### param: ElementHandle.evalOnSelector.selector = %%-query-selector-%% ### param: ElementHandle.evalOnSelector.expression = %%-evaluate-expression-%% ### param: ElementHandle.evalOnSelector.arg - `arg` <[EvaluationArgument]> Optional argument to pass to [`param: expression`]. ## async method: ElementHandle.evalOnSelectorAll * langs: - alias-python: eval_on_selector_all - alias-js: $$eval - returns: <[Serializable]> Returns the return value of [`param: expression`]. The method finds all elements matching the specified selector in the `ElementHandle`'s subtree and passes an array of matched elements as a first argument to [`param: expression`]. See [Working with selectors](./selectors.md) for more details. If [`param: expression`] returns a [Promise], then [`method: ElementHandle.evalOnSelectorAll`] would wait for the promise to resolve and return its value. Examples: ```html
Hello!
Hi!
``` ```js const feedHandle = await page.$('.feed'); expect(await feedHandle.$$eval('.tweet', nodes => nodes.map(n => n.innerText))).toEqual(['Hello!', 'Hi!']); ``` ```java ElementHandle feedHandle = page.querySelector(".feed"); assertEquals(Arrays.asList("Hello!", "Hi!"), feedHandle.evalOnSelectorAll(".tweet", "nodes => nodes.map(n => n.innerText)")); ``` ```python async feed_handle = await page.query_selector(".feed") assert await feed_handle.eval_on_selector_all(".tweet", "nodes => nodes.map(n => n.innerText)") == ["hello!", "hi!"] ``` ```python sync feed_handle = page.query_selector(".feed") assert feed_handle.eval_on_selector_all(".tweet", "nodes => nodes.map(n => n.innerText)") == ["hello!", "hi!"] ``` ### param: ElementHandle.evalOnSelectorAll.selector = %%-query-selector-%% ### param: ElementHandle.evalOnSelectorAll.expression = %%-evaluate-expression-%% ### param: ElementHandle.evalOnSelectorAll.arg - `arg` <[EvaluationArgument]> Optional argument to pass to [`param: expression`]. ## async method: ElementHandle.fill This method waits for [actionability](./actionability.md) checks, focuses the element, fills it and triggers an `input` event after filling. If the element is inside the `