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. A locator can be created with the Page.Locator() method.
Methods
AllAsync
Added in: v1.29When the locator points to a list of elements, this returns an array of locators, pointing to their respective elements.
Locator.AllAsync() does not wait for elements to match the locator, and instead immediately returns whatever is present in the page.
When the list of elements changes dynamically, Locator.AllAsync() will produce unpredictable and flaky results.
When the list of elements is stable, but loaded dynamically, wait for the full list to finish loading before calling Locator.AllAsync().
Usage
foreach (var li in await page.GetByRole("listitem").AllAsync())
await li.ClickAsync();
Returns
AllInnerTextsAsync
Added in: v1.14Returns an array of node.innerText
values for all matching nodes.
If you need to assert text on the page, prefer Expect(Locator).ToHaveTextAsync() with UseInnerText option to avoid flakiness. See assertions guide for more details.
Usage
var texts = await page.GetByRole(AriaRole.Link).AllInnerTextsAsync();
Returns
AllTextContentsAsync
Added in: v1.14Returns an array of node.textContent
values for all matching nodes.
If you need to assert text on the page, prefer Expect(Locator).ToHaveTextAsync() to avoid flakiness. See assertions guide for more details.
Usage
var texts = await page.GetByRole(AriaRole.Link).AllTextContentsAsync();
Returns
And
Added in: v1.34Creates a locator that matches both this locator and the argument locator.
Usage
The following example finds a button with a specific title.
var button = page.GetByRole(AriaRole.Button).And(page.GetByTitle("Subscribe"));
Arguments
Returns
BlurAsync
Added in: v1.28Calls blur on the element.
Usage
await Locator.BlurAsync(options);
Arguments
options
LocatorBlurOptions?
(optional)-
Timeout
[float]? (optional)#Maximum time in milliseconds. Defaults to
30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the BrowserContext.SetDefaultTimeout() or Page.SetDefaultTimeout() methods.
-
Returns
BoundingBoxAsync
Added in: v1.14This method returns the bounding box of the element matching the locator, 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.
Usage
var box = await page.GetByRole(AriaRole.Button).BoundingBoxAsync();
await page.Mouse.ClickAsync(box.X + box.Width / 2, box.Y + box.Height / 2);
Arguments
options
LocatorBoundingBoxOptions?
(optional)-
Timeout
[float]? (optional)#Maximum time in milliseconds. Defaults to
30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the BrowserContext.SetDefaultTimeout() or Page.SetDefaultTimeout() methods.
-
Returns
- BoundingBox?#
-
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.
-
Details
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.
CheckAsync
Added in: v1.14Ensure that checkbox or radio element is checked.
Usage
await page.GetByRole(AriaRole.Checkbox).CheckAsync();
Arguments
options
LocatorCheckOptions?
(optional)-
Whether to bypass the actionability checks. Defaults to
false
. -
Deprecated
This option has no effect.
This option has no effect.
-
Position
Position? (optional)#-
X
[float] -
Y
[float]
A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
-
-
Timeout
[float]? (optional)#Maximum time in milliseconds. Defaults to
30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the BrowserContext.SetDefaultTimeout() or Page.SetDefaultTimeout() methods. -
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
Details
Performs the following steps:
- 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.
- Wait for actionability checks on the element, unless Force option is set.
- Scroll the element into view if needed.
- Use Page.Mouse to click in the center of the element.
- 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 Timeout, this method throws a TimeoutError. Passing zero timeout disables this.
ClearAsync
Added in: v1.28Clear the input field.
Usage
await page.GetByRole(AriaRole.Textbox).ClearAsync();
Arguments
options
LocatorClearOptions?
(optional)-
Whether to bypass the actionability checks. Defaults to
false
. -
Deprecated
This option has no effect.
This option has no effect.
-
Timeout
[float]? (optional)#Maximum time in milliseconds. Defaults to
30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the BrowserContext.SetDefaultTimeout() or Page.SetDefaultTimeout() methods.
-
Returns
Details
This method waits for actionability checks, focuses the element, clears it and triggers an input
event after clearing.
If the target element is not an <input>
, <textarea>
or [contenteditable]
element, this method throws an error. However, if the element is inside the <label>
element that has an associated control, the control will be cleared instead.
ClickAsync
Added in: v1.14Click an element.
Usage
Click a button:
await page.GetByRole(AriaRole.Button).ClickAsync();
Shift-right-click at a specific position on a canvas:
await page.Locator("canvas").ClickAsync(new() {
Button = MouseButton.Right,
Modifiers = new[] { KeyboardModifier.Shift },
Position = new Position { X = 0, Y = 0 }
});
Arguments
options
LocatorClickOptions?
(optional)-
Button
enum MouseButton { Left, Right, Middle }?
(optional)#Defaults to
left
. -
defaults to 1. See UIEvent.detail.
-
Delay
[float]? (optional)#Time to wait between
mousedown
andmouseup
in milliseconds. Defaults to 0. -
Whether to bypass the actionability checks. Defaults to
false
. -
Modifiers
IEnumerable?<enum KeyboardModifier { Alt, Control, ControlOrMeta, Meta, Shift }
> (optional)#Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used. "ControlOrMeta" resolves to "Control" on Windows and Linux and to "Meta" on macOS.
-
Deprecated
This option will default to
true
in the future.Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. Defaults to
false
. -
Position
Position? (optional)#-
X
[float] -
Y
[float]
A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
-
-
Timeout
[float]? (optional)#Maximum time in milliseconds. Defaults to
30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the BrowserContext.SetDefaultTimeout() or Page.SetDefaultTimeout() methods. -
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. Note that keyboardmodifiers
will be pressed regardless oftrial
to allow testing elements which are only visible when those keys are pressed.
-
Returns
Details
This method clicks the element by performing the following steps:
- Wait for actionability checks on the element, unless Force option is set.
- Scroll the element into view if needed.
- Use Page.Mouse to click in the center of the element, or the specified Position.
- Wait for initiated navigations to either succeed or fail, unless 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 Timeout, this method throws a TimeoutError. Passing zero timeout disables this.
ContentFrame
Added in: v1.43Returns a FrameLocator object pointing to the same iframe
as this locator.
Useful when you have a Locator object obtained somewhere, and later on would like to interact with the content inside the frame.
For a reverse operation, use FrameLocator.Owner.
Usage
var locator = Page.Locator("iframe[name=\"embedded\"]");
// ...
var frameLocator = locator.ContentFrame;
await frameLocator.GetByRole(AriaRole.Button).ClickAsync();
Returns
CountAsync
Added in: v1.14Returns the number of elements matching the locator.
If you need to assert the number of elements on the page, prefer Expect(Locator).ToHaveCountAsync() to avoid flakiness. See assertions guide for more details.
Usage
int count = await page.GetByRole(AriaRole.Listitem).CountAsync();
Returns
DblClickAsync
Added in: v1.14Double-click an element.
Usage
await Locator.DblClickAsync(options);
Arguments
options
LocatorDblClickOptions?
(optional)-
Button
enum MouseButton { Left, Right, Middle }?
(optional)#Defaults to
left
. -
Delay
[float]? (optional)#Time to wait between
mousedown
andmouseup
in milliseconds. Defaults to 0. -
Whether to bypass the actionability checks. Defaults to
false
. -
Modifiers
IEnumerable?<enum KeyboardModifier { Alt, Control, ControlOrMeta, Meta, Shift }
> (optional)#Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used. "ControlOrMeta" resolves to "Control" on Windows and Linux and to "Meta" on macOS.
-
Deprecated
This option has no effect.
This option has no effect.
-
Position
Position? (optional)#-
X
[float] -
Y
[float]
A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
-
-
Timeout
[float]? (optional)#Maximum time in milliseconds. Defaults to
30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the BrowserContext.SetDefaultTimeout() or Page.SetDefaultTimeout() methods. -
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. Note that keyboardmodifiers
will be pressed regardless oftrial
to allow testing elements which are only visible when those keys are pressed.
-
Returns
Details
This method double clicks the element by performing the following steps:
- Wait for actionability checks on the element, unless Force option is set.
- Scroll the element into view if needed.
- Use Page.Mouse to double click in the center of the element, or the specified Position.
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 Timeout, this method throws a TimeoutError. Passing zero timeout disables this.
element.dblclick()
dispatches two click
events and a single dblclick
event.