public interface Locator
Page.locator()
method.
Locator locator = page.locator("text=Submit");
locator.click();
The difference between the Locator and ElementHandle
is that the latter points to a particular element, while Locator
captures the logic of how to retrieve that element.
In the example below, handle points to a particular DOM element on page. If that element changes text or is used by React to render an entirely different component, handle is still pointing to that very DOM element. This can lead to unexpected behaviors.
ElementHandle handle = page.querySelector("text=Submit");
handle.hover();
handle.click();
With the locator, every time the element
is used, up-to-date DOM element is located in the page using the selector. So
in the snippet below, underlying DOM element is going to be located twice.
Locator locator = page.locator("text=Submit");
locator.hover();
locator.click();
**Strictness**
Locators are strict. This means that all operations on locators that imply some target DOM element will throw if more than one element matches given selector.
// Throws if there are several buttons in DOM:
page.locator("button").click();
// Works because we explicitly tell locator to pick the first element:
page.locator("button").first().click();
// Works because count knows what to do with multiple matches:
page.locator("button").count();
Modifier and Type | Method and Description |
---|---|
List<String> |
allInnerTexts()
Returns an array of
node.innerText values for all matching nodes. |
List<String> |
allTextContents()
Returns an array of
node.textContent values for all matching nodes. |
default BoundingBox |
boundingBox()
This method returns the bounding box of the element, or
null if the element is not visible. |
BoundingBox |
boundingBox(Locator.BoundingBoxOptions options)
This method returns the bounding box of the element, or
null if the element is not visible. |
default void |
check()
This method checks the element by performing the following steps:
Ensure that element is a checkbox or a radio input.
|
void |
check(Locator.CheckOptions options)
This method checks the element by performing the following steps:
Ensure that element is a checkbox or a radio input.
|
default void |
click()
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.
|
void |
click(Locator.ClickOptions options)
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.
|
int |
count()
Returns the number of elements matching given selector.
|
default void |
dblclick()
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 .
Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set. |
void |
dblclick(Locator.DblclickOptions options)
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 .
Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set. |
default void |
dispatchEvent(String type)
The snippet below dispatches the
click event on the element. |
default void |
dispatchEvent(String type,
Object eventInit)
The snippet below dispatches the
click event on the element. |
void |
dispatchEvent(String type,
Object eventInit,
Locator.DispatchEventOptions options)
The snippet below dispatches the
click event on the element. |
default ElementHandle |
elementHandle()
Resolves given locator to the first matching DOM element.
|
ElementHandle |
elementHandle(Locator.ElementHandleOptions options)
Resolves given locator to the first matching DOM element.
|
List<ElementHandle> |
elementHandles()
Resolves given locator to all matching DOM elements.
|
default Object |
evaluate(String expression)
Returns the return value of
expression . |
default Object |
evaluate(String expression,
Object arg)
Returns the return value of
expression . |
Object |
evaluate(String expression,
Object arg,
Locator.EvaluateOptions options)
Returns the return value of
expression . |
default Object |
evaluateAll(String expression)
The method finds all elements matching the specified locator and passes an array of matched elements as a first argument
to
expression . |
Object |
evaluateAll(String expression,
Object arg)
The method finds all elements matching the specified locator and passes an array of matched elements as a first argument
to
expression . |
default JSHandle |
evaluateHandle(String expression)
Returns the return value of
expression as a JSHandle . |
default JSHandle |
evaluateHandle(String expression,
Object arg)
Returns the return value of
expression as a JSHandle . |
JSHandle |
evaluateHandle(String expression,
Object arg,
Locator.EvaluateHandleOptions options)
Returns the return value of
expression as a JSHandle . |
default void |
fill(String value)
This method waits for actionability checks, focuses the
element, fills it and triggers an
input event after filling. |
void |
fill(String value,
Locator.FillOptions options)
This method waits for actionability checks, focuses the
element, fills it and triggers an
input event after filling. |
Locator |
first()
Returns locator to the first matching element.
|
default void |
focus()
Calls focus on the element.
|
void |
focus(Locator.FocusOptions options)
Calls focus on the element.
|
default String |
getAttribute(String name)
Returns element attribute value.
|
String |
getAttribute(String name,
Locator.GetAttributeOptions options)
Returns element attribute value.
|
default void |
hover()
This method hovers over 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 hover over the center of the element, or the specified position .
Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.
|
void |
hover(Locator.HoverOptions options)
This method hovers over 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 hover over the center of the element, or the specified position .
Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.
|
default String |
innerHTML()
Returns the
element.innerHTML . |
String |
innerHTML(Locator.InnerHTMLOptions options)
Returns the
element.innerHTML . |
default String |
innerText()
Returns the
element.innerText . |
String |
innerText(Locator.InnerTextOptions options)
Returns the
element.innerText . |
default String |
inputValue()
Returns
input.value for <input> or <textarea> or <select> element. |
String |
inputValue(Locator.InputValueOptions options)
Returns
input.value for <input> or <textarea> or <select> element. |
default boolean |
isChecked()
Returns whether the element is checked.
|
boolean |
isChecked(Locator.IsCheckedOptions options)
Returns whether the element is checked.
|
default boolean |
isDisabled()
Returns whether the element is disabled, the opposite of enabled.
|
boolean |
isDisabled(Locator.IsDisabledOptions options)
Returns whether the element is disabled, the opposite of enabled.
|
default boolean |
isEditable()
Returns whether the element is editable.
|
boolean |
isEditable(Locator.IsEditableOptions options)
Returns whether the element is editable.
|
default boolean |
isEnabled()
Returns whether the element is enabled.
|
boolean |
isEnabled(Locator.IsEnabledOptions options)
Returns whether the element is enabled.
|
default boolean |
isHidden()
Returns whether the element is hidden, the opposite of visible.
|
boolean |
isHidden(Locator.IsHiddenOptions options)
Returns whether the element is hidden, the opposite of visible.
|
default boolean |
isVisible()
Returns whether the element is visible.
|
boolean |
isVisible(Locator.IsVisibleOptions options)
Returns whether the element is visible.
|
Locator |
last()
Returns locator to the last matching element.
|
Locator |
locator(String selector)
The method finds an element matching the specified selector in the
Locator 's subtree. |
Locator |
nth(int index)
Returns locator to the n-th matching element.
|
default void |
press(String key)
Focuses the element, and then uses
Keyboard.down() and Keyboard.up() . |
void |
press(String key,
Locator.PressOptions options)
Focuses the element, and then uses
Keyboard.down() and Keyboard.up() . |
default byte[] |
screenshot()
Returns the buffer with the captured screenshot.
|
byte[] |
screenshot(Locator.ScreenshotOptions options)
Returns the buffer with the captured screenshot.
|
default void |
scrollIntoViewIfNeeded()
This method waits for actionability checks, then tries to
scroll element into view, unless it is completely visible as defined by IntersectionObserver's
ratio . |
void |
scrollIntoViewIfNeeded(Locator.ScrollIntoViewIfNeededOptions options)
This method waits for actionability checks, then tries to
scroll element into view, unless it is completely visible as defined by IntersectionObserver's
ratio . |
default List<String> |
selectOption(ElementHandle values)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
default List<String> |
selectOption(ElementHandle[] values)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
List<String> |
selectOption(ElementHandle[] values,
Locator.SelectOptionOptions options)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
List<String> |
selectOption(ElementHandle values,
Locator.SelectOptionOptions options)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
default List<String> |
selectOption(SelectOption values)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
default List<String> |
selectOption(SelectOption[] values)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
List<String> |
selectOption(SelectOption[] values,
Locator.SelectOptionOptions options)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
List<String> |
selectOption(SelectOption values,
Locator.SelectOptionOptions options)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
default List<String> |
selectOption(String values)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
default List<String> |
selectOption(String[] values)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
List<String> |
selectOption(String[] values,
Locator.SelectOptionOptions options)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
List<String> |
selectOption(String values,
Locator.SelectOptionOptions options)
This method waits for actionability checks, waits until
all specified options are present in the
<select> element and selects these options. |
default void |
selectText()
This method waits for actionability checks, then focuses
the element and selects all its text content.
|
void |
selectText(Locator.SelectTextOptions options)
This method waits for actionability checks, then focuses
the element and selects all its text content.
|
default void |
setChecked(boolean checked)
This method checks or unchecks an element by performing the following steps:
Ensure that matched element is a checkbox or a radio input.
|
void |
setChecked(boolean checked,
Locator.SetCheckedOptions options)
This method checks or unchecks an element by performing the following steps:
Ensure that matched element is a checkbox or a radio input.
|
default void |
setInputFiles(FilePayload files)
This method expects
element to point to an input element. |
default void |
setInputFiles(FilePayload[] files)
This method expects
element to point to an input element. |
void |
setInputFiles(FilePayload[] files,
Locator.SetInputFilesOptions options)
This method expects
element to point to an input element. |
void |
setInputFiles(FilePayload files,
Locator.SetInputFilesOptions options)
This method expects
element to point to an input element. |
default void |
setInputFiles(Path files)
This method expects
element to point to an input element. |
default void |
setInputFiles(Path[] files)
This method expects
element to point to an input element. |
void |
setInputFiles(Path[] files,
Locator.SetInputFilesOptions options)
This method expects
element to point to an input element. |
void |
setInputFiles(Path files,
Locator.SetInputFilesOptions options)
This method expects
element to point to an input element. |
default void |
tap()
This method taps 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.touchscreen() to tap the center of the element, or the specified position .
Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.
|
void |
tap(Locator.TapOptions options)
This method taps 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.touchscreen() to tap the center of the element, or the specified position .
Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.
|
default String |
textContent()
Returns the
node.textContent . |
String |
textContent(Locator.TextContentOptions options)
Returns the
node.textContent . |
default void |
type(String text)
Focuses the element, and then sends a
keydown , keypress /input , and keyup event for each character in the text. |
void |
type(String text,
Locator.TypeOptions options)
Focuses the element, and then sends a
keydown , keypress /input , and keyup event for each character in the text. |
default void |
uncheck()
This method checks the element by performing the following steps:
Ensure that element is a checkbox or a radio input.
|
void |
uncheck(Locator.UncheckOptions options)
This method checks the element by performing the following steps:
Ensure that element is a checkbox or a radio input.
|
List<String> allInnerTexts()
node.innerText
values for all matching nodes.List<String> allTextContents()
node.textContent
values for all matching nodes.default BoundingBox boundingBox()
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.
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.
BoundingBox box = element.boundingBox();
page.mouse().click(box.x + box.width / 2, box.y + box.height / 2);
BoundingBox boundingBox(Locator.BoundingBoxOptions options)
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.
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.
BoundingBox box = element.boundingBox();
page.mouse().click(box.x + box.width / 2, box.y + box.height / 2);
default void check()
force
option is set.Page.mouse()
to click in the center of the element.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.
void check(Locator.CheckOptions options)
force
option is set.Page.mouse()
to click in the center of the element.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.
default void click()
force
option is set.Page.mouse()
to click in the center of the element, or the specified position
.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.
void click(Locator.ClickOptions options)
force
option is set.Page.mouse()
to click in the center of the element, or the specified position
.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.
int count()
default void dblclick()
force
option is set.Page.mouse()
to double click in the center of the element, or the specified position
.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 timeout
, this method throws a TimeoutError
. Passing
zero timeout disables this.
NOTE: element.dblclick()
dispatches two click
events and a single dblclick
event.
void dblclick(Locator.DblclickOptions options)
force
option is set.Page.mouse()
to double click in the center of the element, or the specified position
.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 timeout
, this method throws a TimeoutError
. Passing
zero timeout disables this.
NOTE: element.dblclick()
dispatches two click
events and a single dblclick
event.
default void dispatchEvent(String type, Object eventInit)
click
event on the element. Regardless of the visibility state of the element,
click
is dispatched. This is equivalent to calling element.click().
element.dispatchEvent("click");
Under the hood, it creates an instance of an event based on the given type
, initializes it with eventInit
properties
and dispatches it on the element. Events are composed
, cancelable
and bubble by default.
Since eventInit
is event-specific, please refer to the events documentation for the lists of initial properties:
You can also specify JSHandle
as the property value if you want live objects to be passed into the event:
// Note you can only create DataTransfer in Chromium and Firefox
JSHandle dataTransfer = page.evaluateHandle("() => new DataTransfer()");
Map<String, Object> arg = new HashMap<>();
arg.put("dataTransfer", dataTransfer);
element.dispatchEvent("dragstart", arg);
type
- DOM event type: "click"
, "dragstart"
, etc.eventInit
- Optional event-specific initialization properties.default void dispatchEvent(String type)
click
event on the element. Regardless of the visibility state of the element,
click
is dispatched. This is equivalent to calling element.click().
element.dispatchEvent("click");
Under the hood, it creates an instance of an event based on the given type
, initializes it with eventInit
properties
and dispatches it on the element. Events are composed
, cancelable
and bubble by default.
Since eventInit
is event-specific, please refer to the events documentation for the lists of initial properties:
You can also specify JSHandle
as the property value if you want live objects to be passed into the event:
// Note you can only create DataTransfer in Chromium and Firefox
JSHandle dataTransfer = page.evaluateHandle("() => new DataTransfer()");
Map<String, Object> arg = new HashMap<>();
arg.put("dataTransfer", dataTransfer);
element.dispatchEvent("dragstart", arg);
type
- DOM event type: "click"
, "dragstart"
, etc.void dispatchEvent(String type, Object eventInit, Locator.DispatchEventOptions options)
click
event on the element. Regardless of the visibility state of the element,
click
is dispatched. This is equivalent to calling element.click().
element.dispatchEvent("click");
Under the hood, it creates an instance of an event based on the given type
, initializes it with eventInit
properties
and dispatches it on the element. Events are composed
, cancelable
and bubble by default.
Since eventInit
is event-specific, please refer to the events documentation for the lists of initial properties:
You can also specify JSHandle
as the property value if you want live objects to be passed into the event:
// Note you can only create DataTransfer in Chromium and Firefox
JSHandle dataTransfer = page.evaluateHandle("() => new DataTransfer()");
Map<String, Object> arg = new HashMap<>();
arg.put("dataTransfer", dataTransfer);
element.dispatchEvent("dragstart", arg);
type
- DOM event type: "click"
, "dragstart"
, etc.eventInit
- Optional event-specific initialization properties.default ElementHandle elementHandle()
ElementHandle elementHandle(Locator.ElementHandleOptions options)
List<ElementHandle> elementHandles()
default Object evaluate(String expression, Object arg)
expression
.
This method passes this handle as the first argument to expression
.
If expression
returns a Promise, then
handle.evaluate
would wait for the promise to resolve and return its value.
Examples:
Locator tweets = page.locator(".tweet .retweets");
assertEquals("10 retweets", tweets.evaluate("node => node.innerText"));
expression
- JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted
as a function. Otherwise, evaluated as an expression.arg
- Optional argument to pass to expression
.default Object evaluate(String expression)
expression
.
This method passes this handle as the first argument to expression
.
If expression
returns a Promise, then
handle.evaluate
would wait for the promise to resolve and return its value.
Examples:
Locator tweets = page.locator(".tweet .retweets");
assertEquals("10 retweets", tweets.evaluate("node => node.innerText"));
expression
- JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted
as a function. Otherwise, evaluated as an expression.Object evaluate(String expression, Object arg, Locator.EvaluateOptions options)
expression
.
This method passes this handle as the first argument to expression
.
If expression
returns a Promise, then
handle.evaluate
would wait for the promise to resolve and return its value.
Examples:
Locator tweets = page.locator(".tweet .retweets");
assertEquals("10 retweets", tweets.evaluate("node => node.innerText"));
expression
- JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted
as a function. Otherwise, evaluated as an expression.arg
- Optional argument to pass to expression
.default Object evaluateAll(String expression)
expression
. Returns the result of expression
invocation.
If expression
returns a Promise, then Locator.evaluateAll()
would wait for the promise to resolve and return its value.
Examples:
Locator elements = page.locator("div");
boolean divCounts = (boolean) elements.evaluateAll("(divs, min) => divs.length >= min", 10);
expression
- JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted
as a function. Otherwise, evaluated as an expression.Object evaluateAll(String expression, Object arg)
expression
. Returns the result of expression
invocation.
If expression
returns a Promise, then Locator.evaluateAll()
would wait for the promise to resolve and return its value.
Examples:
Locator elements = page.locator("div");
boolean divCounts = (boolean) elements.evaluateAll("(divs, min) => divs.length >= min", 10);
expression
- JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted
as a function. Otherwise, evaluated as an expression.arg
- Optional argument to pass to expression
.default JSHandle evaluateHandle(String expression, Object arg)
expression
as a JSHandle
.
This method passes this handle as the first argument to expression
.
The only difference between Locator.evaluate()
and Locator.evaluateHandle()
is that Locator.evaluateHandle()
returns JSHandle
.
If the function passed to the Locator.evaluateHandle()
returns a Promise, then Locator.evaluateHandle()
would wait for the promise to resolve and return its value.
See Page.evaluateHandle()
for more details.
expression
- JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted
as a function. Otherwise, evaluated as an expression.arg
- Optional argument to pass to expression
.default JSHandle evaluateHandle(String expression)
expression
as a JSHandle
.
This method passes this handle as the first argument to expression
.
The only difference between Locator.evaluate()
and Locator.evaluateHandle()
is that Locator.evaluateHandle()
returns JSHandle
.
If the function passed to the Locator.evaluateHandle()
returns a Promise, then Locator.evaluateHandle()
would wait for the promise to resolve and return its value.
See Page.evaluateHandle()
for more details.
expression
- JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted
as a function. Otherwise, evaluated as an expression.JSHandle evaluateHandle(String expression, Object arg, Locator.EvaluateHandleOptions options)
expression
as a JSHandle
.
This method passes this handle as the first argument to expression
.
The only difference between Locator.evaluate()
and Locator.evaluateHandle()
is that Locator.evaluateHandle()
returns JSHandle
.
If the function passed to the Locator.evaluateHandle()
returns a Promise, then Locator.evaluateHandle()
would wait for the promise to resolve and return its value.
See Page.evaluateHandle()
for more details.
expression
- JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted
as a function. Otherwise, evaluated as an expression.arg
- Optional argument to pass to expression
.default void fill(String value)
input
event after filling. Note that you can pass an empty string to clear the input
field.
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 filled
instead.
To send fine-grained keyboard events, use Locator.type()
.
value
- Value to set for the <input>
, <textarea>
or [contenteditable]
element.void fill(String value, Locator.FillOptions options)
input
event after filling. Note that you can pass an empty string to clear the input
field.
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 filled
instead.
To send fine-grained keyboard events, use Locator.type()
.
value
- Value to set for the <input>
, <textarea>
or [contenteditable]
element.Locator first()
default void focus()
void focus(Locator.FocusOptions options)
default String getAttribute(String name)
name
- Attribute name to get the value for.String getAttribute(String name, Locator.GetAttributeOptions options)
name
- Attribute name to get the value for.default void hover()
force
option is set.Page.mouse()
to hover over the center of the element, or the specified position
.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.
void hover(Locator.HoverOptions options)
force
option is set.Page.mouse()
to hover over the center of the element, or the specified position
.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.
default String innerHTML()
element.innerHTML
.String innerHTML(Locator.InnerHTMLOptions options)
element.innerHTML
.default String innerText()
element.innerText
.String innerText(Locator.InnerTextOptions options)
element.innerText
.default String inputValue()
input.value
for <input>
or <textarea>
or <select>
element. Throws for non-input elements.String inputValue(Locator.InputValueOptions options)
input.value
for <input>
or <textarea>
or <select>
element. Throws for non-input elements.default boolean isChecked()
boolean isChecked(Locator.IsCheckedOptions options)
default boolean isDisabled()
boolean isDisabled(Locator.IsDisabledOptions options)
default boolean isEditable()
boolean isEditable(Locator.IsEditableOptions options)
default boolean isEnabled()
boolean isEnabled(Locator.IsEnabledOptions options)
default boolean isHidden()
boolean isHidden(Locator.IsHiddenOptions options)
default boolean isVisible()
boolean isVisible(Locator.IsVisibleOptions options)
Locator last()
Locator locator(String selector)
Locator
's subtree. See Working with selectors for more details.selector
- A selector to use when resolving DOM element. See working with
selectors for more details.Locator nth(int index)
default void press(String key)
Keyboard.down()
and Keyboard.up()
.
key
can specify the intended keyboardEvent.key value or a single
character to generate the text for. A superset of the key
values can be found here. Examples of the keys are:
F1
- F12
, Digit0
- Digit9
, KeyA
- KeyZ
, Backquote
, Minus
, Equal
, Backslash
, Backspace
, Tab
,
Delete
, Escape
, ArrowDown
, End
, Enter
, Home
, Insert
, PageDown
, PageUp
, ArrowRight
, ArrowUp
, etc.
Following modification shortcuts are also supported: Shift
, Control
, Alt
, Meta
, ShiftLeft
.
Holding down Shift
will type the text that corresponds to the key
in the upper case.
If key
is a single character, it is case-sensitive, so the values a
and A
will generate different respective
texts.
Shortcuts such as key: "Control+o"
or key: "Control+Shift+T"
are supported as well. When specified with the
modifier, modifier is pressed and being held while the subsequent key is being pressed.
key
- Name of the key to press or a character to generate, such as ArrowLeft
or a
.void press(String key, Locator.PressOptions options)
Keyboard.down()
and Keyboard.up()
.
key
can specify the intended keyboardEvent.key value or a single
character to generate the text for. A superset of the key
values can be found here. Examples of the keys are:
F1
- F12
, Digit0
- Digit9
, KeyA
- KeyZ
, Backquote
, Minus
, Equal
, Backslash
, Backspace
, Tab
,
Delete
, Escape
, ArrowDown
, End
, Enter
, Home
, Insert
, PageDown
, PageUp
, ArrowRight
, ArrowUp
, etc.
Following modification shortcuts are also supported: Shift
, Control
, Alt
, Meta
, ShiftLeft
.
Holding down Shift
will type the text that corresponds to the key
in the upper case.
If key
is a single character, it is case-sensitive, so the values a
and A
will generate different respective
texts.
Shortcuts such as key: "Control+o"
or key: "Control+Shift+T"
are supported as well. When specified with the
modifier, modifier is pressed and being held while the subsequent key is being pressed.
key
- Name of the key to press or a character to generate, such as ArrowLeft
or a
.default byte[] screenshot()
This method waits for the actionability checks, then scrolls element into view before taking a screenshot. If the element is detached from DOM, the method throws an error.
byte[] screenshot(Locator.ScreenshotOptions options)
This method waits for the actionability checks, then scrolls element into view before taking a screenshot. If the element is detached from DOM, the method throws an error.
default void scrollIntoViewIfNeeded()
ratio
.void scrollIntoViewIfNeeded(Locator.ScrollIntoViewIfNeededOptions options)
ratio
.default List<String> selectOption(String values)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.List<String> selectOption(String values, Locator.SelectOptionOptions options)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.default List<String> selectOption(ElementHandle values)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.List<String> selectOption(ElementHandle values, Locator.SelectOptionOptions options)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.default List<String> selectOption(String[] values)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.List<String> selectOption(String[] values, Locator.SelectOptionOptions options)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.default List<String> selectOption(SelectOption values)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.List<String> selectOption(SelectOption values, Locator.SelectOptionOptions options)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.default List<String> selectOption(ElementHandle[] values)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.List<String> selectOption(ElementHandle[] values, Locator.SelectOptionOptions options)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.default List<String> selectOption(SelectOption[] values)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.List<String> selectOption(SelectOption[] values, Locator.SelectOptionOptions options)
<select>
element and selects these options.
If the target element is not a <select>
element, this method throws an error. However, if the element is inside the
<label>
element that has an associated control, the control will be used
instead.
Returns the array of option values that have been successfully selected.
Triggers a change
and input
event once all the provided options have been selected.
// single selection matching the value
element.selectOption("blue");
// single selection matching the label
element.selectOption(new SelectOption().setLabel("Blue"));
// multiple selection
element.selectOption(new String[] {"red", "green", "blue"});
values
- Options to select. If the <select>
has the multiple
attribute, all matching options are selected, otherwise only the
first option matching one of the passed options is selected. String values are equivalent to {value:'string'}
. Option
is considered matching if all specified properties match.default void selectText()
void selectText(Locator.SelectTextOptions options)
default void setChecked(boolean checked)
force
option is set. If the element is detached during the checks, the whole action is retried.Page.mouse()
to click in the center of the element.noWaitAfter
option is set. When all steps combined have not finished during the specified timeout
, this method throws a TimeoutError
. Passing
zero timeout disables this.
checked
- Whether to check or uncheck the checkbox.void setChecked(boolean checked, Locator.SetCheckedOptions options)
force
option is set. If the element is detached during the checks, the whole action is retried.Page.mouse()
to click in the center of the element.noWaitAfter
option is set. When all steps combined have not finished during the specified timeout
, this method throws a TimeoutError
. Passing
zero timeout disables this.
checked
- Whether to check or uncheck the checkbox.default void setInputFiles(Path files)
element
to point to an input element.
Sets the value of the file input to these file paths or files. If some of the filePaths
are relative paths, then they
are resolved relative to the the current working directory. For empty array, clears the selected files.
void setInputFiles(Path files, Locator.SetInputFilesOptions options)
element
to point to an input element.
Sets the value of the file input to these file paths or files. If some of the filePaths
are relative paths, then they
are resolved relative to the the current working directory. For empty array, clears the selected files.
default void setInputFiles(Path[] files)
element
to point to an input element.
Sets the value of the file input to these file paths or files. If some of the filePaths
are relative paths, then they
are resolved relative to the the current working directory. For empty array, clears the selected files.
void setInputFiles(Path[] files, Locator.SetInputFilesOptions options)
element
to point to an input element.
Sets the value of the file input to these file paths or files. If some of the filePaths
are relative paths, then they
are resolved relative to the the current working directory. For empty array, clears the selected files.
default void setInputFiles(FilePayload files)
element
to point to an input element.
Sets the value of the file input to these file paths or files. If some of the filePaths
are relative paths, then they
are resolved relative to the the current working directory. For empty array, clears the selected files.
void setInputFiles(FilePayload files, Locator.SetInputFilesOptions options)
element
to point to an input element.
Sets the value of the file input to these file paths or files. If some of the filePaths
are relative paths, then they
are resolved relative to the the current working directory. For empty array, clears the selected files.
default void setInputFiles(FilePayload[] files)
element
to point to an input element.
Sets the value of the file input to these file paths or files. If some of the filePaths
are relative paths, then they
are resolved relative to the the current working directory. For empty array, clears the selected files.
void setInputFiles(FilePayload[] files, Locator.SetInputFilesOptions options)
element
to point to an input element.
Sets the value of the file input to these file paths or files. If some of the filePaths
are relative paths, then they
are resolved relative to the the current working directory. For empty array, clears the selected files.
default void tap()
force
option is set.Page.touchscreen()
to tap the center of the element, or the specified position
.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.
NOTE: element.tap()
requires that the hasTouch
option of the browser context be set to true.
void tap(Locator.TapOptions options)
force
option is set.Page.touchscreen()
to tap the center of the element, or the specified position
.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.
NOTE: element.tap()
requires that the hasTouch
option of the browser context be set to true.
default String textContent()
node.textContent
.String textContent(Locator.TextContentOptions options)
node.textContent
.default void type(String text)
keydown
, keypress
/input
, and keyup
event for each character in the text.
To press a special key, like Control
or ArrowDown
, use Locator.press()
.
element.type("Hello"); // Types instantly
element.type("World", new Locator.TypeOptions().setDelay(100)); // Types slower, like a user
An example of typing into a text field and then submitting the form:
Locator element = page.locator("input");
element.type("some text");
element.press("Enter");
text
- A text to type into a focused element.void type(String text, Locator.TypeOptions options)
keydown
, keypress
/input
, and keyup
event for each character in the text.
To press a special key, like Control
or ArrowDown
, use Locator.press()
.
element.type("Hello"); // Types instantly
element.type("World", new Locator.TypeOptions().setDelay(100)); // Types slower, like a user
An example of typing into a text field and then submitting the form:
Locator element = page.locator("input");
element.type("some text");
element.press("Enter");
text
- A text to type into a focused element.default void uncheck()
force
option is set.Page.mouse()
to click in the center of the element.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.
void uncheck(Locator.UncheckOptions options)
force
option is set.Page.mouse()
to click in the center of the element.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.
Copyright © 2021. All rights reserved.