class Playwright::Locator
- Learn more about locators](../locators.md).
a way to find element(s) on the page at any moment. A locator can be created with the [‘method: Page.locator` -
method.
Locators are the central piece of Playwright’s auto-waiting and retry-ability. In a nutshell, locators represent
def all
li.click();
for li in page.get_by_role('listitem').all():
```python sync
**Usage**
When the list of elements is stable, but loaded dynamically, wait for the full list to finish loading before calling [`method: Locator.all`].
When the list of elements changes dynamically, [`method: Locator.all`] will produce unpredictable and flaky results.
**NOTE**: [`method: Locator.all`] does not wait for elements to match the locator, and instead immediately returns whatever is present in the page.
When the locator points to a list of elements, this returns an array of locators, pointing to their respective elements.
def all wrap_impl(@impl.all) end
def all_inner_texts
texts = page.get_by_role("link").all_inner_texts()
```python sync
**Usage**
**NOTE**: If you need to assert text on the page, prefer [`method: LocatorAssertions.toHaveText`] with `useInnerText` option to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns an array of `node.innerText` values for all matching nodes.
def all_inner_texts wrap_impl(@impl.all_inner_texts) end
def all_text_contents
texts = page.get_by_role("link").all_text_contents()
```python sync
**Usage**
**NOTE**: If you need to assert text on the page, prefer [`method: LocatorAssertions.toHaveText`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns an array of `node.textContent` values for all matching nodes.
def all_text_contents wrap_impl(@impl.all_text_contents) end
def and(locator)
button = page.get_by_role("button").and_(page.getByTitle("Subscribe"))
```python sync
The following example finds a button with a specific title.
**Usage**
Creates a locator that matches both this locator and the argument locator.
def and(locator) wrap_impl(@impl.and(unwrap_impl(locator))) end
def blur(timeout: nil)
def blur(timeout: nil) wrap_impl(@impl.blur(timeout: unwrap_impl(timeout))) end
def bounding_box(timeout: nil)
page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)
box = page.get_by_role("button").bounding_box()
```python sync
**Usage**
snippet should click the center of the element.
Assuming the page is static, it is safe to use bounding box coordinates to perform input. For example, the following
[Element.getBoundingClientRect](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect).
Elements from child frames return the bounding box relative to the main frame, unlike the
means `x` and/or `y` may be negative.
[Element.getBoundingClientRect](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect). That
Scrolling affects the returned bounding box, similarly to
**Details**
calculated relative to the main frame viewport - which is usually the same as the browser window.
This method returns the bounding box of the element matching the locator, or `null` if the element is not visible. The bounding box is
def bounding_box(timeout: nil) wrap_impl(@impl.bounding_box(timeout: unwrap_impl(timeout))) end
def check(
page.get_by_role("checkbox").check()
```python sync
**Usage**
`TimeoutError`. Passing zero timeout disables this.
When all steps combined have not finished during the specified `timeout`, this method throws a
If the element is detached from the DOM at any moment during the action, this method throws.
1. Ensure that the element is now checked. If not, this method throws.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
1. Use [`property: Page.mouse`] to click in the center of the element.
1. Scroll the element into view if needed.
1. Wait for [actionability](../actionability.md) checks on the element, unless `force` option is set.
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.
Performs the following steps:
**Details**
Ensure that checkbox or radio element is checked.
def check( force: nil, noWaitAfter: nil, position: nil, timeout: nil, trial: nil) wrap_impl(@impl.check(force: unwrap_impl(force), noWaitAfter: unwrap_impl(noWaitAfter), position: unwrap_impl(position), timeout: unwrap_impl(timeout), trial: unwrap_impl(trial))) end
def checked?(timeout: nil)
checked = page.get_by_role("checkbox").is_checked()
```python sync
**Usage**
**NOTE**: If you need to assert that checkbox is checked, prefer [`method: LocatorAssertions.toBeChecked`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns whether the element is checked. Throws if the element is not a checkbox or radio input.
def checked?(timeout: nil) wrap_impl(@impl.checked?(timeout: unwrap_impl(timeout))) end
def clear(force: nil, noWaitAfter: nil, timeout: nil)
page.get_by_role("textbox").clear()
```python sync
**Usage**
If the target element is not an ``, `
def clear(force: nil, noWaitAfter: nil, timeout: nil) wrap_impl(@impl.clear(force: unwrap_impl(force), noWaitAfter: unwrap_impl(noWaitAfter), timeout: unwrap_impl(timeout))) end
def click(
)
button="right", modifiers=["Shift"], position={"x": 23, "y": 32}
page.locator("canvas").click(
```python sync
Shift-right-click at a specific position on a canvas:
```
page.get_by_role("button").click()
```python sync
Click a button:
**Usage**
`TimeoutError`. Passing zero timeout disables this.
When all steps combined have not finished during the specified `timeout`, this method throws a
If the element is detached from the DOM at any moment during the action, this method throws.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
1. Use [`property: Page.mouse`] to click in the center of the element, or the specified `position`.
1. Scroll the element into view if needed.
1. Wait for [actionability](../actionability.md) checks on the element, unless `force` option is set.
This method clicks the element by performing the following steps:
**Details**
Click an element.
def click( button: nil, clickCount: nil, delay: nil, force: nil, modifiers: nil, noWaitAfter: nil, position: nil, timeout: nil, trial: nil) wrap_impl(@impl.click(button: unwrap_impl(button), clickCount: unwrap_impl(clickCount), delay: unwrap_impl(delay), force: unwrap_impl(force), modifiers: unwrap_impl(modifiers), noWaitAfter: unwrap_impl(noWaitAfter), position: unwrap_impl(position), timeout: unwrap_impl(timeout), trial: unwrap_impl(trial))) end
def count
count = page.get_by_role("listitem").count()
```python sync
**Usage**
**NOTE**: If you need to assert the number of elements on the page, prefer [`method: LocatorAssertions.toHaveCount`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns the number of elements matching the locator.
def count wrap_impl(@impl.count) end
def dblclick(
`TimeoutError`. Passing zero timeout disables this.
When all steps combined have not finished during the specified `timeout`, this method throws a
If the element is detached from the DOM at any moment during the action, this method throws.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set. Note that if the first click of the `dblclick()` triggers a navigation event, this method will throw.
1. Use [`property: Page.mouse`] to double click in the center of the element, or the specified `position`.
1. Scroll the element into view if needed.
1. Wait for [actionability](../actionability.md) checks on the element, unless `force` option is set.
This method double clicks the element by performing the following steps:
**Details**
Double-click an element.
def dblclick( button: nil, delay: nil, force: nil, modifiers: nil, noWaitAfter: nil, position: nil, timeout: nil, trial: nil) wrap_impl(@impl.dblclick(button: unwrap_impl(button), delay: unwrap_impl(delay), force: unwrap_impl(force), modifiers: unwrap_impl(modifiers), noWaitAfter: unwrap_impl(noWaitAfter), position: unwrap_impl(position), timeout: unwrap_impl(timeout), trial: unwrap_impl(trial))) end
def disabled?(timeout: nil)
disabled = page.get_by_role("button").is_disabled()
```python sync
**Usage**
**NOTE**: If you need to assert that an element is disabled, prefer [`method: LocatorAssertions.toBeDisabled`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns whether the element is disabled, the opposite of [enabled](../actionability.md#enabled).
def disabled?(timeout: nil) wrap_impl(@impl.disabled?(timeout: unwrap_impl(timeout))) end
def dispatch_event(type, eventInit: nil, timeout: nil)
locator.dispatch_event("#source", "dragstart", {"dataTransfer": data_transfer})
data_transfer = page.evaluate_handle("new DataTransfer()")
# note you can only create data_transfer in chromium and firefox
```python sync
You can also specify `JSHandle` as the property value if you want live objects to be passed into the event:
- [WheelEvent](https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/WheelEvent)
- [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent)
- [PointerEvent](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent)
- [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent)
- [KeyboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent)
- [FocusEvent](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/FocusEvent)
- [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event)
- [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent/DragEvent)
- [DeviceOrientationEvent](https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent/DeviceOrientationEvent)
- [DeviceMotionEvent](https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent/DeviceMotionEvent)
properties:
Since `eventInit` is event-specific, please refer to the events documentation for the lists of initial
default.
`eventInit` properties and dispatches it on the element. Events are `composed`, `cancelable` and bubble by
Under the hood, it creates an instance of an event based on the given `type`, initializes it with
[element.click()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click).
is dispatched. This is equivalent to calling
The snippet above dispatches the `click` event on the element. Regardless of the visibility state of the element, `click`
**Details**
```
locator.dispatch_event("click")
```python sync
**Usage**
Programmatically dispatch an event on the matching element.
def dispatch_event(type, eventInit: nil, timeout: nil) wrap_impl(@impl.dispatch_event(unwrap_impl(type), eventInit: unwrap_impl(eventInit), timeout: unwrap_impl(timeout))) end
def drag_to(
)
target_position={"x": 10, "y": 20}
source_position={"x": 34, "y": 7},
target,
source.drag_to(
# or specify exact positions relative to the top-left corners of the elements:
source.drag_to(target)
target = page.locator("#target")
source = page.locator("#source")
```python sync
**Usage**
element or position and perform a `mouseup`.
first move to the source element, perform a `mousedown`, then move to the target
This method drags the locator to another target locator or target position. It will
**Details**
Drag the source element towards the target element and drop it.
def drag_to( target, force: nil, noWaitAfter: nil, sourcePosition: nil, targetPosition: nil, timeout: nil, trial: nil) wrap_impl(@impl.drag_to(unwrap_impl(target), force: unwrap_impl(force), noWaitAfter: unwrap_impl(noWaitAfter), sourcePosition: unwrap_impl(sourcePosition), targetPosition: unwrap_impl(targetPosition), timeout: unwrap_impl(timeout), trial: unwrap_impl(trial))) end
def editable?(timeout: nil)
editable = page.get_by_role("textbox").is_editable()
```python sync
**Usage**
**NOTE**: If you need to assert that an element is editable, prefer [`method: LocatorAssertions.toBeEditable`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns whether the element is [editable](../actionability.md#editable).
def editable?(timeout: nil) wrap_impl(@impl.editable?(timeout: unwrap_impl(timeout))) end
def element_handle(timeout: nil)
def element_handle(timeout: nil) wrap_impl(@impl.element_handle(timeout: unwrap_impl(timeout))) end
def element_handles
def element_handles wrap_impl(@impl.element_handles) end
def enabled?(timeout: nil)
enabled = page.get_by_role("button").is_enabled()
```python sync
**Usage**
**NOTE**: If you need to assert that an element is enabled, prefer [`method: LocatorAssertions.toBeEnabled`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns whether the element is [enabled](../actionability.md#enabled).
def enabled?(timeout: nil) wrap_impl(@impl.enabled?(timeout: unwrap_impl(timeout))) end
def evaluate(expression, arg: nil, timeout: nil)
assert tweets.evaluate("node => node.innerText") == "10 retweets"
tweets = page.locator(".tweet .retweets")
```python sync
**Usage**
If `expression` throws or rejects, this method throws.
If `expression` returns a [Promise], this method will wait for the promise to resolve and return its value.
Returns the return value of `expression`, called with the matching element as a first argument, and `arg` as a second argument.
**Details**
Execute JavaScript code in the page, taking the matching element as an argument.
def evaluate(expression, arg: nil, timeout: nil) wrap_impl(@impl.evaluate(unwrap_impl(expression), arg: unwrap_impl(arg), timeout: unwrap_impl(timeout))) end
def evaluate_all(expression, arg: nil)
more_than_ten = locator.evaluate_all("(divs, min) => divs.length > min", 10)
locator = page.locator("div")
```python sync
**Usage**
If `expression` throws or rejects, this method throws.
If `expression` returns a [Promise], this method will wait for the promise to resolve and return its value.
Returns the return value of `expression`, called with an array of all matching elements as a first argument, and `arg` as a second argument.
**Details**
Execute JavaScript code in the page, taking all matching elements as an argument.
def evaluate_all(expression, arg: nil) wrap_impl(@impl.evaluate_all(unwrap_impl(expression), arg: unwrap_impl(arg))) end
def evaluate_handle(expression, arg: nil, timeout: nil)
If `expression` throws or rejects, this method throws.
If `expression` returns a [Promise], this method will wait for the promise to resolve and return its value.
The only difference between [`method: Locator.evaluate`] and [`method: Locator.evaluateHandle`] is that [`method: Locator.evaluateHandle`] returns `JSHandle`.
Returns the return value of `expression` as a`JSHandle`, called with the matching element as a first argument, and `arg` as a second argument.
**Details**
Execute JavaScript code in the page, taking the matching element as an argument, and return a `JSHandle` with the result.
def evaluate_handle(expression, arg: nil, timeout: nil) wrap_impl(@impl.evaluate_handle(unwrap_impl(expression), arg: unwrap_impl(arg), timeout: unwrap_impl(timeout))) end
def expect(expression, options)
def expect(expression, options) wrap_impl(@impl.expect(unwrap_impl(expression), unwrap_impl(options))) end
def fill(value, force: nil, noWaitAfter: nil, timeout: nil)
If the target element is not an ``, `
def fill(value, force: nil, noWaitAfter: nil, timeout: nil) wrap_impl(@impl.fill(unwrap_impl(value), force: unwrap_impl(force), noWaitAfter: unwrap_impl(noWaitAfter), timeout: unwrap_impl(timeout))) end
def filter(has: nil, hasNot: nil, hasNotText: nil, hasText: nil)
).screenshot()
has=page.get_by_role("button", name="column 2 button")
row_locator.filter(has_text="text in column 1").filter(
# ...
row_locator = page.locator("tr")
```python sync
**Usage**
It can be chained to filter multiple times.
This method narrows existing locator according to the options, for example filters by text.
def filter(has: nil, hasNot: nil, hasNotText: nil, hasText: nil) wrap_impl(@impl.filter(has: unwrap_impl(has), hasNot: unwrap_impl(hasNot), hasNotText: unwrap_impl(hasNotText), hasText: unwrap_impl(hasText))) end
def first
def first wrap_impl(@impl.first) end
def focus(timeout: nil)
def focus(timeout: nil) wrap_impl(@impl.focus(timeout: unwrap_impl(timeout))) end
def frame_locator(selector)
locator.click()
locator = page.frame_locator("iframe").get_by_text("Submit")
```python sync
**Usage**
in that iframe:
When working with iframes, you can create a frame locator that will enter the iframe and allow locating elements
def frame_locator(selector) wrap_impl(@impl.frame_locator(unwrap_impl(selector))) end
def get_attribute(name, timeout: nil)
Returns the matching element's attribute value.
def get_attribute(name, timeout: nil) wrap_impl(@impl.get_attribute(unwrap_impl(name), timeout: unwrap_impl(timeout))) end
def get_by_alt_text(text, exact: nil)
page.get_by_alt_text("Playwright logo").click()
```python sync
```
```html
For example, this method will find the image by alt text "Playwright logo":
**Usage**
Allows locating elements by their alt text.
def get_by_alt_text(text, exact: nil) wrap_impl(@impl.get_by_alt_text(unwrap_impl(text), exact: unwrap_impl(exact))) end
def get_by_label(text, exact: nil)
page.get_by_label("Password").fill("secret")
page.get_by_label("Username").fill("john")
```python sync
```
```html
For example, this method will find inputs by label "Username" and "Password" in the following DOM:
**Usage**
Allows locating input elements by the text of the associated `
def get_by_label(text, exact: nil) wrap_impl(@impl.get_by_label(unwrap_impl(text), exact: unwrap_impl(exact))) end
def get_by_placeholder(text, exact: nil)
page.get_by_placeholder("name@example.com").fill("playwright@microsoft.com")
```python sync
You can fill the input after locating it by the placeholder text:
```
```html
For example, consider the following DOM structure.
**Usage**
Allows locating input elements by the placeholder text.
def get_by_placeholder(text, exact: nil) wrap_impl(@impl.get_by_placeholder(unwrap_impl(text), exact: unwrap_impl(exact))) end
def get_by_role(
Role selector **does not replace** accessibility audits and conformance tests, but rather gives early feedback about the ARIA guidelines.
**Details**
```
page.get_by_role("button", name=re.compile("submit", re.IGNORECASE)).click()
page.get_by_role("checkbox", name="Subscribe").check()
expect(page.get_by_role("heading", name="Sign up")).to_be_visible()
```python sync
You can locate each element by it's implicit role:
```
Subscribe
def get_by_role( role, checked: nil, disabled: nil, exact: nil, expanded: nil, includeHidden: nil, level: nil, name: nil, pressed: nil, selected: nil) wrap_impl(@impl.get_by_role(unwrap_impl(role), checked: unwrap_impl(checked), disabled: unwrap_impl(disabled), exact: unwrap_impl(exact), expanded: unwrap_impl(expanded), includeHidden: unwrap_impl(includeHidden), level: unwrap_impl(level), name: unwrap_impl(name), pressed: unwrap_impl(pressed), selected: unwrap_impl(selected))) end
def get_by_test_id(testId)
**Details**
```
page.get_by_test_id("directions").click()
```python sync
You can locate the element by it's test id:
```
```html
Consider the following DOM structure.
**Usage**
Locate element by the test id.
def get_by_test_id(testId) wrap_impl(@impl.get_by_test_id(unwrap_impl(testId))) end
def get_by_text(text, exact: nil)
Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.
**Details**
```
page.get_by_text(re.compile("^hello$", re.IGNORECASE))
# Matches second
page.get_by_text(re.compile("Hello"))
# Matches both
s
page.get_by_text("Hello", exact=True)
# Matches second
page.get_by_text("Hello world")
# Matches first
page.get_by_text("world")
# Matches
```python sync
You can locate by text substring, exact string, or a regular expression:
```
```html
Consider the following DOM structure:
**Usage**
See also [`method: Locator.filter`] that allows to match by another criteria, like an accessible role, and then filter by the text content.
Allows locating elements that contain given text.
```
expect(page.get_by_title("Issues count")).to_have_text("25 issues")
```python sync
You can check the issues count after locating it by the title text:
```
25 issues
```html
Consider the following DOM structure.
**Usage**
Allows locating elements by their title attribute.
```
hidden = page.get_by_role("button").is_hidden()
```python sync
**Usage**
**NOTE**: If you need to assert that element is hidden, prefer [`method: LocatorAssertions.toBeHidden`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns whether the element is hidden, the opposite of [visible](../actionability.md#visible).
Highlight the corresponding element(s) on the screen. Useful for debugging, don't commit the code that uses [`method: Locator.highlight`].
`TimeoutError`. Passing zero timeout disables this.
When all steps combined have not finished during the specified `timeout`, this method throws a
If the element is detached from the DOM at any moment during the action, this method throws.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
1. Use [`property: Page.mouse`] to hover over the center of the element, or the specified `position`.
1. Scroll the element into view if needed.
1. Wait for [actionability](../actionability.md) checks on the element, unless `force` option is set.
This method hovers over the element by performing the following steps:
**Details**
```
page.get_by_role("link").hover()
```python sync
**Usage**
Hover over the matching element.
Returns the [`element.innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML).
**NOTE**: If you need to assert text on the page, prefer [`method: LocatorAssertions.toHaveText`] with `useInnerText` option to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns the [`element.innerText`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText).
Throws elements that are not an input, textarea or a select. However, if the element is inside the `
```
banana = page.get_by_role("listitem").last
```python sync
**Usage**
Returns locator to the last matching element.
[Learn more about locators](../locators.md).
The method finds an element matching the specified selector in the locator's subtree. It also accepts filter options, similar to [`method: Locator.filter`] method.
```
banana = page.get_by_role("listitem").nth(2)
```python sync
**Usage**
Returns locator to the n-th matching element. It's zero based, `nth(0)` selects the first element.
```
new_email.click()
page.get_by_role("button", name="Dismiss").click()
if (dialog.is_visible()):
expect(new_email.or_(dialog)).to_be_visible()
dialog = page.get_by_text("Confirm security settings")
new_email = page.get_by_role("button", name="New")
```python sync
Consider a scenario where you'd like to click on a "New email" button, but sometimes a security settings dialog shows up instead. In this case, you can wait for either a "New email" button, or a dialog and act accordingly.
**Usage**
Creates a locator that matches either of the two locators.
A page this locator belongs to.
modifier, modifier is pressed and being held while the subsequent key is being pressed.
Shortcuts such as `key: "Control+o"`, `key: "Control++` or `key: "Control+Shift+T"` are supported as well. When specified with the
respective texts.
If `key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different
Holding down `Shift` will type the text that corresponds to the `key` in the upper case.
Following modification shortcuts are also supported: `Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`.
`Delete`, `Escape`, `ArrowDown`, `End`, `Enter`, `Home`, `Insert`, `PageDown`, `PageUp`, `ArrowRight`, `ArrowUp`, etc.
`F1` - `F12`, `Digit0`- `Digit9`, `KeyA`- `KeyZ`, `Backquote`, `Minus`, `Equal`, `Backslash`, `Backspace`, `Tab`,
[here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the keys are:
generate the text for. A superset of the `key` values can be found
[keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) value or a single character to
`key` can specify the intended
Focuses the element, and then uses [`method: Keyboard.down`] and [`method: Keyboard.up`].
**Details**
```
page.get_by_role("textbox").press("Backspace")
```python sync
**Usage**
Focuses the matching element and presses a combination of the keys.
```
locator.press("Enter")
locator.press_sequentially("my password")
locator = page.get_by_label("Password")
```python sync
An example of typing into a text field and then submitting the form:
```
locator.press_sequentially("world", delay=100) # types slower, like a user
locator.press_sequentially("hello") # types instantly
```python sync
**Usage**
To press a special key, like `Control` or `ArrowDown`, use [`method: Locator.press`].
Focuses the element, and then sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text.
**NOTE**: In most cases, you should use [`method: Locator.fill`] instead. You only need to press keys one by one if there is special keyboard handling on the page.
Returns the buffer with the captured screenshot.
screenshot. If the element is detached from DOM, the method throws an error.
This method waits for the [actionability](../actionability.md) checks, then scrolls element into view before taking a
This method captures a screenshot of the page, clipped to the size and position of a particular element matching the locator. If the element is covered by other elements, it will not be actually visible on the screenshot. If the element is a scrollable container, only the currently scrolled content will be visible on the screenshot.
**Details**
```
page.get_by_role("link").screenshot(animations="disabled", path="link.png")
```python sync
Disable animations and save screenshot to a file:
```
page.get_by_role("link").screenshot()
```python sync
**Usage**
Take a screenshot of the element matching the locator.
[IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)'s `ratio`.
completely visible as defined by
This method waits for [actionability](../actionability.md) checks, then tries to scroll element into view, unless it is
```
element.select_option(value=["red", "green", "blue"])
# multiple selection for blue, red and second option
element.select_option(label="blue")
# single selection matching the label
element.select_option("blue")
# single selection matching the value or label
```python sync
```
page.get_by_text("Hello", exact=True)
# Matches second
page.get_by_text("Hello world")
# Matches first
page.get_by_text("world")
# Matches
```python sync
You can locate by text substring, exact string, or a regular expression:
```
Hello
Hello world
```html
Consider the following DOM structure:
**Usage**
See also [`method: Locator.filter`] that allows to match by another criteria, like an accessible role, and then filter by the text content.
Allows locating elements that contain given text.
def get_by_text(text, exact: nil) wrap_impl(@impl.get_by_text(unwrap_impl(text), exact: unwrap_impl(exact))) end
def get_by_title(text, exact: nil)
expect(page.get_by_title("Issues count")).to_have_text("25 issues")
```python sync
You can check the issues count after locating it by the title text:
```
25 issues
```html
Consider the following DOM structure.
**Usage**
Allows locating elements by their title attribute.
def get_by_title(text, exact: nil) wrap_impl(@impl.get_by_title(unwrap_impl(text), exact: unwrap_impl(exact))) end
def hidden?(timeout: nil)
hidden = page.get_by_role("button").is_hidden()
```python sync
**Usage**
**NOTE**: If you need to assert that element is hidden, prefer [`method: LocatorAssertions.toBeHidden`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns whether the element is hidden, the opposite of [visible](../actionability.md#visible).
def hidden?(timeout: nil) wrap_impl(@impl.hidden?(timeout: unwrap_impl(timeout))) end
def highlight
def highlight wrap_impl(@impl.highlight) end
def hover(
When all steps combined have not finished during the specified `timeout`, this method throws a
If the element is detached from the DOM at any moment during the action, this method throws.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
1. Use [`property: Page.mouse`] to hover over the center of the element, or the specified `position`.
1. Scroll the element into view if needed.
1. Wait for [actionability](../actionability.md) checks on the element, unless `force` option is set.
This method hovers over the element by performing the following steps:
**Details**
```
page.get_by_role("link").hover()
```python sync
**Usage**
Hover over the matching element.
def hover( force: nil, modifiers: nil, noWaitAfter: nil, position: nil, timeout: nil, trial: nil) wrap_impl(@impl.hover(force: unwrap_impl(force), modifiers: unwrap_impl(modifiers), noWaitAfter: unwrap_impl(noWaitAfter), position: unwrap_impl(position), timeout: unwrap_impl(timeout), trial: unwrap_impl(trial))) end
def inner_html(timeout: nil)
def inner_html(timeout: nil) wrap_impl(@impl.inner_html(timeout: unwrap_impl(timeout))) end
def inner_text(timeout: nil)
Returns the [`element.innerText`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText).
def inner_text(timeout: nil) wrap_impl(@impl.inner_text(timeout: unwrap_impl(timeout))) end
def input_value(timeout: nil)
def input_value(timeout: nil) wrap_impl(@impl.input_value(timeout: unwrap_impl(timeout))) end
def last
banana = page.get_by_role("listitem").last
```python sync
**Usage**
Returns locator to the last matching element.
def last wrap_impl(@impl.last) end
def locator(
The method finds an element matching the specified selector in the locator's subtree. It also accepts filter options, similar to [`method: Locator.filter`] method.
def locator( selectorOrLocator, has: nil, hasNot: nil, hasNotText: nil, hasText: nil) wrap_impl(@impl.locator(unwrap_impl(selectorOrLocator), has: unwrap_impl(has), hasNot: unwrap_impl(hasNot), hasNotText: unwrap_impl(hasNotText), hasText: unwrap_impl(hasText))) end
def nth(index)
banana = page.get_by_role("listitem").nth(2)
```python sync
**Usage**
Returns locator to the n-th matching element. It's zero based, `nth(0)` selects the first element.
def nth(index) wrap_impl(@impl.nth(unwrap_impl(index))) end
def or(locator)
new_email.click()
page.get_by_role("button", name="Dismiss").click()
if (dialog.is_visible()):
expect(new_email.or_(dialog)).to_be_visible()
dialog = page.get_by_text("Confirm security settings")
new_email = page.get_by_role("button", name="New")
```python sync
Consider a scenario where you'd like to click on a "New email" button, but sometimes a security settings dialog shows up instead. In this case, you can wait for either a "New email" button, or a dialog and act accordingly.
**Usage**
Creates a locator that matches either of the two locators.
def or(locator) wrap_impl(@impl.or(unwrap_impl(locator))) end
def page
def page wrap_impl(@impl.page) end
def press(key, delay: nil, noWaitAfter: nil, timeout: nil)
Shortcuts such as `key: "Control+o"`, `key: "Control++` or `key: "Control+Shift+T"` are supported as well. When specified with the
respective texts.
If `key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different
Holding down `Shift` will type the text that corresponds to the `key` in the upper case.
Following modification shortcuts are also supported: `Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`.
`Delete`, `Escape`, `ArrowDown`, `End`, `Enter`, `Home`, `Insert`, `PageDown`, `PageUp`, `ArrowRight`, `ArrowUp`, etc.
`F1` - `F12`, `Digit0`- `Digit9`, `KeyA`- `KeyZ`, `Backquote`, `Minus`, `Equal`, `Backslash`, `Backspace`, `Tab`,
[here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the keys are:
generate the text for. A superset of the `key` values can be found
[keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) value or a single character to
`key` can specify the intended
Focuses the element, and then uses [`method: Keyboard.down`] and [`method: Keyboard.up`].
**Details**
```
page.get_by_role("textbox").press("Backspace")
```python sync
**Usage**
Focuses the matching element and presses a combination of the keys.
def press(key, delay: nil, noWaitAfter: nil, timeout: nil) wrap_impl(@impl.press(unwrap_impl(key), delay: unwrap_impl(delay), noWaitAfter: unwrap_impl(noWaitAfter), timeout: unwrap_impl(timeout))) end
def press_sequentially(text, delay: nil, noWaitAfter: nil, timeout: nil)
locator.press("Enter")
locator.press_sequentially("my password")
locator = page.get_by_label("Password")
```python sync
An example of typing into a text field and then submitting the form:
```
locator.press_sequentially("world", delay=100) # types slower, like a user
locator.press_sequentially("hello") # types instantly
```python sync
**Usage**
To press a special key, like `Control` or `ArrowDown`, use [`method: Locator.press`].
Focuses the element, and then sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text.
**NOTE**: In most cases, you should use [`method: Locator.fill`] instead. You only need to press keys one by one if there is special keyboard handling on the page.
def press_sequentially(text, delay: nil, noWaitAfter: nil, timeout: nil) wrap_impl(@impl.press_sequentially(unwrap_impl(text), delay: unwrap_impl(delay), noWaitAfter: unwrap_impl(noWaitAfter), timeout: unwrap_impl(timeout))) end
def screenshot(
screenshot. If the element is detached from DOM, the method throws an error.
This method waits for the [actionability](../actionability.md) checks, then scrolls element into view before taking a
This method captures a screenshot of the page, clipped to the size and position of a particular element matching the locator. If the element is covered by other elements, it will not be actually visible on the screenshot. If the element is a scrollable container, only the currently scrolled content will be visible on the screenshot.
**Details**
```
page.get_by_role("link").screenshot(animations="disabled", path="link.png")
```python sync
Disable animations and save screenshot to a file:
```
page.get_by_role("link").screenshot()
```python sync
**Usage**
Take a screenshot of the element matching the locator.
def screenshot( animations: nil, caret: nil, mask: nil, maskColor: nil, omitBackground: nil, path: nil, quality: nil, scale: nil, style: nil, timeout: nil, type: nil) wrap_impl(@impl.screenshot(animations: unwrap_impl(animations), caret: unwrap_impl(caret), mask: unwrap_impl(mask), maskColor: unwrap_impl(maskColor), omitBackground: unwrap_impl(omitBackground), path: unwrap_impl(path), quality: unwrap_impl(quality), scale: unwrap_impl(scale), style: unwrap_impl(style), timeout: unwrap_impl(timeout), type: unwrap_impl(type))) end
def scroll_into_view_if_needed(timeout: nil)
completely visible as defined by
This method waits for [actionability](../actionability.md) checks, then tries to scroll element into view, unless it is
def scroll_into_view_if_needed(timeout: nil) wrap_impl(@impl.scroll_into_view_if_needed(timeout: unwrap_impl(timeout))) end
def select_option(
element.select_option(value=["red", "green", "blue"])
# multiple selection for blue, red and second option
element.select_option(label="blue")
# single selection matching the label
element.select_option("blue")
# single selection matching the value or label
```python sync
```
def select_option( element: nil, index: nil, value: nil, label: nil, force: nil, noWaitAfter: nil, timeout: nil) wrap_impl(@impl.select_option(element: unwrap_impl(element), index: unwrap_impl(index), value: unwrap_impl(value), label: unwrap_impl(label), force: unwrap_impl(force), noWaitAfter: unwrap_impl(noWaitAfter), timeout: unwrap_impl(timeout))) end
def select_text(force: nil, timeout: nil)
def select_text(force: nil, timeout: nil) wrap_impl(@impl.select_text(force: unwrap_impl(force), timeout: unwrap_impl(timeout))) end
def set_checked(
When all steps combined have not finished during the specified `timeout`, this method throws a
1. Ensure that the element is now checked or unchecked. If not, this method throws.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
1. Use [`property: Page.mouse`] to click in the center of the element.
1. Scroll the element into view if needed.
1. Wait for [actionability](../actionability.md) checks on the matched element, unless `force` option is set. If the element is detached during the checks, the whole action is retried.
1. If the element already has the right checked state, this method returns immediately.
1. Ensure that matched element is a checkbox or a radio input. If not, this method throws.
This method checks or unchecks an element by performing the following steps:
**Details**
```
page.get_by_role("checkbox").set_checked(True)
```python sync
**Usage**
Set the state of a checkbox or a radio element.
def set_checked( checked, force: nil, noWaitAfter: nil, position: nil, timeout: nil, trial: nil) wrap_impl(@impl.set_checked(unwrap_impl(checked), force: unwrap_impl(force), noWaitAfter: unwrap_impl(noWaitAfter), position: unwrap_impl(position), timeout: unwrap_impl(timeout), trial: unwrap_impl(trial))) end
def set_input_files(files, noWaitAfter: nil, timeout: nil)
def set_input_files(files, noWaitAfter: nil, timeout: nil) wrap_impl(@impl.set_input_files(unwrap_impl(files), noWaitAfter: unwrap_impl(noWaitAfter), timeout: unwrap_impl(timeout))) end
def tap_point(
`TimeoutError`. Passing zero timeout disables this.
When all steps combined have not finished during the specified `timeout`, this method throws a
If the element is detached from the DOM at any moment during the action, this method throws.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
1. Use [`property: Page.touchscreen`] to tap the center of the element, or the specified `position`.
1. Scroll the element into view if needed.
1. Wait for [actionability](../actionability.md) checks on the element, unless `force` option is set.
This method taps the element by performing the following steps:
**Details**
Perform a tap gesture on the element matching the locator.
def tap_point( force: nil, modifiers: nil, noWaitAfter: nil, position: nil, timeout: nil, trial: nil) wrap_impl(@impl.tap_point(force: unwrap_impl(force), modifiers: unwrap_impl(modifiers), noWaitAfter: unwrap_impl(noWaitAfter), position: unwrap_impl(position), timeout: unwrap_impl(timeout), trial: unwrap_impl(trial))) end
def text_content(timeout: nil)
Returns the [`node.textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent).
def text_content(timeout: nil) wrap_impl(@impl.text_content(timeout: unwrap_impl(timeout))) end
def to_s
def to_s wrap_impl(@impl.to_s) end
def type(text, delay: nil, noWaitAfter: nil, timeout: nil)
To press a special key, like `Control` or `ArrowDown`, use [`method: Locator.press`].
Focuses the element, and then sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text.
def type(text, delay: nil, noWaitAfter: nil, timeout: nil) wrap_impl(@impl.type(unwrap_impl(text), delay: unwrap_impl(delay), noWaitAfter: unwrap_impl(noWaitAfter), timeout: unwrap_impl(timeout))) end
def uncheck(
When all steps combined have not finished during the specified `timeout`, this method throws a
If the element is detached from the DOM at any moment during the action, this method throws.
1. Ensure that the element is now unchecked. If not, this method throws.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
1. Use [`property: Page.mouse`] to click in the center of the element.
1. Scroll the element into view if needed.
1. Wait for [actionability](../actionability.md) checks on the element, unless `force` option is set.
1. Ensure that element is a checkbox or a radio input. If not, this method throws. If the element is already unchecked, this method returns immediately.
This method unchecks the element by performing the following steps:
**Details**
```
page.get_by_role("checkbox").uncheck()
```python sync
**Usage**
Ensure that checkbox or radio element is unchecked.
def uncheck( force: nil, noWaitAfter: nil, position: nil, timeout: nil, trial: nil) wrap_impl(@impl.uncheck(force: unwrap_impl(force), noWaitAfter: unwrap_impl(noWaitAfter), position: unwrap_impl(position), timeout: unwrap_impl(timeout), trial: unwrap_impl(trial))) end
def visible?(timeout: nil)
visible = page.get_by_role("button").is_visible()
```python sync
**Usage**
**NOTE**: If you need to assert that element is visible, prefer [`method: LocatorAssertions.toBeVisible`] to avoid flakiness. See [assertions guide](../test-assertions.md) for more details.
Returns whether the element is [visible](../actionability.md#visible).
def visible?(timeout: nil) wrap_impl(@impl.visible?(timeout: unwrap_impl(timeout))) end
def wait_for(state: nil, timeout: nil)
order_sent.wait_for()
order_sent = page.locator("#order-sent")
```python sync
**Usage**
`timeout` milliseconds until the condition is met.
If target element already satisfies the condition, the method returns immediately. Otherwise, waits for up to
Returns when element specified by locator satisfies the `state` option.
def wait_for(state: nil, timeout: nil) wrap_impl(@impl.wait_for(state: unwrap_impl(state), timeout: unwrap_impl(timeout))) end