# class: Locator Locators are the central piece of Playwright's auto-waiting and retry-ability. In a nutshell, locators represent a way to find element(s) on the page at any moment. Locator can be created with the [`method: Page.locator`] method. [Learn more about locators](../locators.md). ## async method: Locator.allInnerTexts - returns: <[Array]<[string]>> Returns an array of `node.innerText` values for all matching nodes. ## async method: Locator.allTextContents - returns: <[Array]<[string]>> Returns an array of `node.textContent` values for all matching nodes. ## async method: Locator.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 element.boundingBox(); await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2); ``` ```java BoundingBox box = element.boundingBox(); page.mouse().click(box.x + box.width / 2, box.y + box.height / 2); ``` ```python async box = await element.bounding_box() await page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2) ``` ```python sync box = element.bounding_box() page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2) ``` ```csharp var box = await element.BoundingBoxAsync(); await page.Mouse.ClickAsync(box.X + box.Width / 2, box.Y + box.Height / 2); ``` ### option: Locator.boundingBox.timeout = %%-input-timeout-%% ## async method: Locator.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: Locator.check.position = %%-input-position-%% ### option: Locator.check.force = %%-input-force-%% ### option: Locator.check.noWaitAfter = %%-input-no-wait-after-%% ### option: Locator.check.timeout = %%-input-timeout-%% ### option: Locator.check.trial = %%-input-trial-%% ## async method: Locator.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: Locator.click.button = %%-input-button-%% ### option: Locator.click.clickCount = %%-input-click-count-%% ### option: Locator.click.delay = %%-input-down-up-delay-%% ### option: Locator.click.position = %%-input-position-%% ### option: Locator.click.modifiers = %%-input-modifiers-%% ### option: Locator.click.force = %%-input-force-%% ### option: Locator.click.noWaitAfter = %%-input-no-wait-after-%% ### option: Locator.click.timeout = %%-input-timeout-%% ### option: Locator.click.trial = %%-input-trial-%% ## async method: Locator.count - returns: <[int]> Returns the number of elements matching given selector. ## async method: Locator.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 `element.dblclick()` dispatches two `click` events and a single `dblclick` event. ::: ### option: Locator.dblclick.button = %%-input-button-%% ### option: Locator.dblclick.delay = %%-input-down-up-delay-%% ### option: Locator.dblclick.position = %%-input-position-%% ### option: Locator.dblclick.modifiers = %%-input-modifiers-%% ### option: Locator.dblclick.force = %%-input-force-%% ### option: Locator.dblclick.noWaitAfter = %%-input-no-wait-after-%% ### option: Locator.dblclick.timeout = %%-input-timeout-%% ### option: Locator.dblclick.trial = %%-input-trial-%% ## async method: Locator.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 element.dispatchEvent('click'); ``` ```java element.dispatchEvent("click"); ``` ```python async await element.dispatch_event("click") ``` ```python sync element.dispatch_event("click") ``` ```csharp await element.DispatchEventAsync("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 element.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); element.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.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.dispatch_event("#source", "dragstart", {"dataTransfer": data_transfer}) ``` ```csharp var dataTransfer = await page.EvaluateHandleAsync("() => new DataTransfer()"); await element.DispatchEventAsync("dragstart", new Dictionary { { "dataTransfer", dataTransfer } }); ``` ### param: Locator.dispatchEvent.type - `type` <[string]> DOM event type: `"click"`, `"dragstart"`, etc. ### param: Locator.dispatchEvent.eventInit - `eventInit` ?<[EvaluationArgument]> Optional event-specific initialization properties. ### option: Locator.dispatchEvent.timeout = %%-input-timeout-%% ## async method: Locator.dragTo ### param: Locator.dragTo.target - `target` <[Locator]> Locator of the element to drag to. ### option: Locator.dragTo.force = %%-input-force-%% ### option: Locator.dragTo.noWaitAfter = %%-input-no-wait-after-%% ### option: Locator.dragTo.timeout = %%-input-timeout-%% ### option: Locator.dragTo.trial = %%-input-trial-%% ### option: Locator.dragTo.sourcePosition = %%-input-source-position-%% ### option: Locator.dragTo.targetPosition = %%-input-target-position-%% ## async method: Locator.elementHandle - returns: <[ElementHandle]> Resolves given locator to the first matching DOM element. If no elements matching the query are visible, waits for them up to a given timeout. If multiple elements match the selector, throws. ### option: Locator.elementHandle.timeout = %%-input-timeout-%% ## async method: Locator.elementHandles - returns: <[Array]<[ElementHandle]>> Resolves given locator to all matching DOM elements. ## async method: Locator.evaluate - returns: <[Serializable]> Returns the return value of [`param: expression`]. This method passes this handle as the first argument to [`param: expression`]. If [`param: expression`] returns a [Promise], then `handle.evaluate` would wait for the promise to resolve and return its value. Examples: ```js const tweets = page.locator('.tweet .retweets'); expect(await tweets.evaluate(node => node.innerText)).toBe('10 retweets'); ``` ```java Locator tweets = page.locator(".tweet .retweets"); assertEquals("10 retweets", tweets.evaluate("node => node.innerText")); ``` ```python async tweets = page.locator(".tweet .retweets") assert await tweets.evaluate("node => node.innerText") == "10 retweets" ``` ```python sync tweets = page.locator(".tweet .retweets") assert tweets.evaluate("node => node.innerText") == "10 retweets" ``` ```csharp var tweets = page.Locator(".tweet .retweets"); Assert.AreEqual("10 retweets", await tweets.EvaluateAsync("node => node.innerText")); ``` ### param: Locator.evaluate.expression = %%-evaluate-expression-%% ### param: Locator.evaluate.arg - `arg` ?<[EvaluationArgument]> Optional argument to pass to [`param: expression`]. ### option: Locator.evaluate.timeout = %%-input-timeout-%% ## async method: Locator.evaluateAll - returns: <[Serializable]> The method finds all elements matching the specified locator and passes an array of matched elements as a first argument to [`param: expression`]. Returns the result of [`param: expression`] invocation. If [`param: expression`] returns a [Promise], then [`method: Locator.evaluateAll`] would wait for the promise to resolve and return its value. Examples: ```js const elements = page.locator('div'); const divCounts = await elements.evaluateAll((divs, min) => divs.length >= min, 10); ``` ```java Locator elements = page.locator("div"); boolean divCounts = (boolean) elements.evaluateAll("(divs, min) => divs.length >= min", 10); ``` ```python async elements = page.locator("div") div_counts = await elements("(divs, min) => divs.length >= min", 10) ``` ```python sync elements = page.locator("div") div_counts = elements("(divs, min) => divs.length >= min", 10) ``` ```csharp var elements = page.Locator("div"); var divsCount = await elements.EvaluateAll("(divs, min) => divs.length >= min", 10); ``` ### param: Locator.evaluateAll.expression = %%-evaluate-expression-%% ### param: Locator.evaluateAll.arg - `arg` ?<[EvaluationArgument]> Optional argument to pass to [`param: expression`]. ## async method: Locator.evaluateHandle - returns: <[JSHandle]> Returns the return value of [`param: expression`] as a [JSHandle]. This method passes this handle as the first argument to [`param: expression`]. The only difference between [`method: Locator.evaluate`] and [`method: Locator.evaluateHandle`] is that [`method: Locator.evaluateHandle`] returns [JSHandle]. If the function passed to the [`method: Locator.evaluateHandle`] returns a [Promise], then [`method: Locator.evaluateHandle`] would wait for the promise to resolve and return its value. See [`method: Page.evaluateHandle`] for more details. ### param: Locator.evaluateHandle.expression = %%-evaluate-expression-%% ### param: Locator.evaluateHandle.arg - `arg` ?<[EvaluationArgument]> Optional argument to pass to [`param: expression`]. ### option: Locator.evaluateHandle.timeout = %%-input-timeout-%% ## async method: Locator.fill This method waits for [actionability](../actionability.md) checks, focuses the element, fills it and triggers an `input` event after filling. Note that you can pass an empty string to clear the input field. If the target element is not an ``, `