class Playwright::ElementHandle
“‘
locator.click()
locator.hover()
locator = page.get_by_text(“Submit”)
“`python sync
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.
“`
handle.click()
handle.hover()
handle = page.query_selector(“text=Submit”)
“`python sync
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.
The difference between the `Locator` and ElementHandle is that the ElementHandle points to a particular element, while `Locator` captures the logic of how to retrieve an element.
ElementHandle instances can be used as an argument in [`method: Page.evalOnSelector`] and [`method: Page.evaluate`] methods.
[`method: JSHandle.dispose`]. ElementHandles are auto-disposed when their origin frame gets navigated.
ElementHandle prevents DOM element from garbage collection unless the handle is disposed with
“`
href_element.click()
href_element = page.query_selector(“a”)
“`python sync
NOTE: The use of ElementHandle is discouraged, use `Locator` objects and web-first assertions instead.
ElementHandle represents an in-page DOM element. ElementHandles can be created with the [`method: Page.querySelector`] method.
- extends: `JSHandle`
def bounding_box
page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)
box = element_handle.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
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, or `null` if the element is not visible. The bounding box is
def bounding_box wrap_impl(@impl.bounding_box) end
def check(
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.
This method checks the element by performing the following steps:
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?
def checked? wrap_impl(@impl.checked?) end
def click(
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:
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 content_frame
def content_frame wrap_impl(@impl.content_frame) 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:
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?
def disabled? wrap_impl(@impl.disabled?) end
def dispatch_event(type, eventInit: nil)
element_handle.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_handle.dispatch_event("click")
```python sync
**Usage**
[element.click()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click).
is dispatched. This is equivalent to calling
The snippet below dispatches the `click` event on the element. Regardless of the visibility state of the element, `click`
def dispatch_event(type, eventInit: nil) wrap_impl(@impl.dispatch_event(unwrap_impl(type), eventInit: unwrap_impl(eventInit))) end
def editable?
def editable? wrap_impl(@impl.editable?) end
def enabled?
def enabled? wrap_impl(@impl.enabled?) end
def eval_on_selector(selector, expression, arg: nil)
assert tweet_handle.eval_on_selector(".retweets", "node => node.innerText") == "10"
assert tweet_handle.eval_on_selector(".like", "node => node.innerText") == "100"
tweet_handle = page.query_selector(".tweet")
```python sync
**Usage**
value.
If `expression` returns a [Promise], then [`method: ElementHandle.evalOnSelector`] would wait for the promise to resolve and return its
argument to `expression`. If no elements match the selector, the method throws an error.
The method finds an element matching the specified selector in the `ElementHandle`s subtree and passes it as a first
Returns the return value of `expression`.
def eval_on_selector(selector, expression, arg: nil) wrap_impl(@impl.eval_on_selector(unwrap_impl(selector), unwrap_impl(expression), arg: unwrap_impl(arg))) end
def eval_on_selector_all(selector, expression, arg: nil)
assert feed_handle.eval_on_selector_all(".tweet", "nodes => nodes.map(n => n.innerText)") == ["hello!", "hi!"]
feed_handle = page.query_selector(".feed")
```python sync
```
```html
**Usage**
value.
If `expression` returns a [Promise], then [`method: ElementHandle.evalOnSelectorAll`] would wait for the promise to resolve and return its
matched elements as a first argument to `expression`.
The method finds all elements matching the specified selector in the `ElementHandle`'s subtree and passes an array of
Returns the return value of `expression`.
def eval_on_selector_all(selector, expression, arg: nil) wrap_impl(@impl.eval_on_selector_all(unwrap_impl(selector), unwrap_impl(expression), arg: unwrap_impl(arg))) end
def event_emitter_proxy
def event_emitter_proxy _emitter_proxy ||= EventEmitterProxy.new(self, @impl)
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 focus
def focus wrap_impl(@impl.focus) end
def get_attribute(name)
def get_attribute(name) wrap_impl(@impl.get_attribute(unwrap_impl(name))) end
def hidden?
def hidden? wrap_impl(@impl.hidden?) 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:
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
def inner_html wrap_impl(@impl.inner_html) end
def inner_text
def inner_text wrap_impl(@impl.inner_text) end
def input_value(timeout: nil)
def input_value(timeout: nil) wrap_impl(@impl.input_value(timeout: unwrap_impl(timeout))) end
def off(event, callback)
-- inherited from EventEmitter --
def off(event, callback) event_emitter_proxy.off(event, callback) end
def on(event, callback)
-- inherited from EventEmitter --
def on(event, callback) event_emitter_proxy.on(event, callback) end
def once(event, callback)
-- inherited from EventEmitter --
def once(event, callback) event_emitter_proxy.once(event, callback) end
def owner_frame
def owner_frame wrap_impl(@impl.owner_frame) end
def press(key, delay: nil, noWaitAfter: nil, timeout: nil)
Shortcuts such as `key: "Control+o"` 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`].
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 query_selector(selector)
The method finds an element matching the specified selector in the `ElementHandle`'s subtree. If no elements match the selector,
def query_selector(selector) wrap_impl(@impl.query_selector(unwrap_impl(selector))) end
def query_selector_all(selector)
The method finds all elements matching the specified selector in the `ElementHandle`s subtree. If no elements match the selector,
def query_selector_all(selector) wrap_impl(@impl.query_selector_all(unwrap_impl(selector))) 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 this particular element. 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.
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)
Throws when `elementHandle` does not point to an element
[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
def scroll_into_view_if_needed(timeout: nil) wrap_impl(@impl.scroll_into_view_if_needed(timeout: unwrap_impl(timeout))) end
def select_option(
handle.select_option(value=["red", "green", "blue"])
# multiple selection
handle.select_option(label="blue")
# single selection matching both the label
handle.select_option("blue")
# Single selection matching the value or label
```python sync
**Usage**
Triggers a `change` and `input` event once all the provided options have been selected.
Returns the array of option values that have been successfully selected.
If the target element is not a `
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 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:
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:
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
def text_content wrap_impl(@impl.text_content) end
def type(text, delay: nil, noWaitAfter: nil, timeout: nil)
To press a special key, like `Control` or `ArrowDown`, use [`method: ElementHandle.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 checks the element by performing the following steps:
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?
def visible? wrap_impl(@impl.visible?) end
def wait_for_element_state(state, timeout: nil)
- `"editable"` Wait until the element is [editable](../actionability.md#editable).
- `"disabled"` Wait until the element is [not enabled](../actionability.md#enabled).
- `"enabled"` Wait until the element is [enabled](../actionability.md#enabled).
- `"stable"` Wait until the element is both [visible](../actionability.md#visible) and [stable](../actionability.md#stable).
- `"hidden"` Wait until the element is [not visible](../actionability.md#visible) or not attached. Note that waiting for hidden does not throw when the element detaches.
- `"visible"` Wait until the element is [visible](../actionability.md#visible).
to pass. This method throws when the element is detached while waiting, unless waiting for the `"hidden"` state.
Depending on the `state` parameter, this method waits for one of the [actionability](../actionability.md) checks
Returns when the element satisfies the `state`.
def wait_for_element_state(state, timeout: nil) wrap_impl(@impl.wait_for_element_state(unwrap_impl(state), timeout: unwrap_impl(timeout))) end
def wait_for_selector(selector, state: nil, strict: nil, timeout: nil)
```
span = div.wait_for_selector("span", state="attached")
# waiting for the "span" selector relative to the div.
div = page.query_selector("div")
page.set_content("
```python sync
**Usage**
`timeout` milliseconds, the function will throw.
satisfies the condition, the method will return immediately. If the selector doesn't satisfy the condition for the
appear/disappear from dom, or become visible/hidden). If at the moment of calling the method `selector` already
Wait for the `selector` relative to the element handle to satisfy `state` option (either
or `detached`.
Returns element specified by selector when it satisfies `state` option. Returns `null` if waiting for `hidden`
def wait_for_selector(selector, state: nil, strict: nil, timeout: nil) wrap_impl(@impl.wait_for_selector(unwrap_impl(selector), state: unwrap_impl(state), strict: unwrap_impl(strict), timeout: unwrap_impl(timeout))) end