module Playwright::ElementHandle

Overview

ElementHandle represents an in-page DOM element. ElementHandles can be created with the page.$(selector) method.

ElementHandle prevents DOM element from garbage collection unless the handle is disposed with jsHandle.dispose(). ElementHandles are auto-disposed when their origin frame gets navigated. ElementHandle instances can be used as an argument in page.$eval(selector, pageFunction[, arg]) and page.evaluate(pageFunction[, arg]) methods.

Included Modules

Defined in:

playwright/elementhandle.cr

Instance Method Summary

Instance methods inherited from module Playwright::JSHandle

as_element : ElementHandle | Nil as_element, dispose : Nil dispose, evaluate(page_function : String, arg : Array(Any) | Nil) : Any
evaluate(page_function : String) : Any
evaluate(page_function : String, *arg : Any) : Any
evaluate
, evaluate_handle(page_function : String, arg : Array(Any) | Nil) : JSHandle
evaluate_handle(page_function : String) : JSHandle
evaluate_handle(page_function : String, *arg : Any) : JSHandle
evaluate_handle
, get_properties : Hash(String, JSHandle) get_properties, get_property(property_name : String) : JSHandle get_property, json_value : Any json_value

Instance Method Detail

abstract def bounding_box : BoundingBox | Nil #

This method returns the bounding box of the element, or null if the element is not visible. The bounding box is calculated relative to the main frame viewport - which is usually the same as the browser window. Scrolling affects the returned bonding box, similarly to Element.getBoundingClientRect. 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.


[View source]
abstract def check(options : CheckOptions | Nil) : Nil #

This method checks the element by performing the following steps:

Ensure that element is a checkbox or a radio input. If not, this method rejects. If the element is already checked, this method returns immediately. Wait for actionability checks on the element, unless force option is set. Scroll the element into view if needed. Use page.mouse to click in the center of the element. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set. Ensure that the element is now checked. If not, this method rejects.

If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified timeout, this method rejects with a TimeoutError. Passing zero timeout disables this.


[View source]
def check : Nil #

[View source]
abstract def click(options : ClickOptions | Nil) : Nil #

This method clicks the element by performing the following steps:

Wait for actionability checks on the element, unless force option is set. Scroll the element into view if needed. Use page.mouse to click in the center of the element, or the specified position. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.

If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified timeout, this method rejects with a TimeoutError. Passing zero timeout disables this.


[View source]
def click : Nil #

[View source]
abstract def content_frame : Frame | Nil #

Returns the content frame for element handles referencing iframe nodes, or null otherwise


[View source]
abstract def dblclick(options : DblclickOptions | Nil) : Nil #

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. Note that if the first click of the #dblclick() triggers a navigation event, this method will reject.

If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified timeout, this method rejects with a TimeoutError. Passing zero timeout disables this.

NOTE elementHandle.dblclick() dispatches two #click events and a single #dblclick event.


[View source]
def dblclick : Nil #

[View source]
abstract def dispatch_event(type : String, event_init : Array(Any) | Nil) : Nil #

The snippet below dispatches the #click event on the element. Regardless of the visibility state of the elment, #click is dispatched. This is equivalend to calling element.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:

DragEvent FocusEvent KeyboardEvent MouseEvent PointerEvent TouchEvent Event

You can also specify JSHandle as the property value if you want live objects to be passed into the event:


[View source]
def dispatch_event(type : String) : Nil #

[View source]
def dispatch_event(type : String, *event_init : Any) : Nil #

[View source]
abstract def eval_on_selector(selector : String, page_function : String, arg : Array(Any) | Nil) : Any #

Returns the return value of pageFunction The method finds an element matching the specified selector in the ElementHandles subtree and passes it as a first argument to pageFunction. See Working with selectors for more details. If no elements match the selector, the method throws an error. If pageFunction returns a Promise, then frame.$eval would wait for the promise to resolve and return its value. Examples:


[View source]
def eval_on_selector(selector : String, page_function : String) : Any #

[View source]
def eval_on_selector(selector : String, page_function : String, *arg : Any) : Any #

[View source]
abstract def eval_on_selector_all(selector : String, page_function : String, arg : Array(Any) | Nil) : Any #

Returns the return value of pageFunction The method finds all elements matching the specified selector in the ElementHandle's subtree and passes an array of matched elements as a first argument to pageFunction. See Working with selectors for more details. If pageFunction returns a Promise, then frame.$$eval would wait for the promise to resolve and return its value. Examples:


[View source]
def eval_on_selector_all(selector : String, page_function : String) : Any #

[View source]
def eval_on_selector_all(selector : String, page_function : String, *arg : Any) : Any #

[View source]
abstract def fill(value : String, options : FillOptions | Nil) : Nil #

This method waits for actionability checks, focuses the element, fills it and triggers an input event after filling. If the element is not an <input>, <textarea> or [contenteditable] element, this method throws an error. Note that you can pass an empty string to clear the input field.


[View source]
def fill(value : String) : Nil #

[View source]
abstract def focus : Nil #

Calls focus on the element.


[View source]
abstract def get_attribute(name : String) : String | Nil #

Returns element attribute value.


[View source]
abstract def hover(options : HoverOptions | Nil) : Nil #

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.

If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified timeout, this method rejects with a TimeoutError. Passing zero timeout disables this.


[View source]
def hover : Nil #

[View source]
abstract def inner_html : String #

Returns the element.innerHTML.


[View source]
abstract def inner_text : String #

Returns the element.innerText.


[View source]
abstract def owner_frame : Frame | Nil #

Returns the frame containing the given element.


[View source]
abstract def press(key : String, options : PressOptions | Nil) : Nil #

Focuses the element, and then uses keyboard.down(key) and keyboard.up(key). 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 suported: 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 speficied with the modifier, modifier is pressed and being held while the subsequent key is being pressed.


[View source]
def press(key : String) : Nil #

[View source]
abstract def query_selector(selector : String) : ElementHandle | Nil #

The method finds an element matching the specified selector in the ElementHandle's subtree. See Working with selectors for more details. If no elements match the selector, returns null.


[View source]
abstract def query_selector_all(selector : String) : Array(ElementHandle) #

The method finds all elements matching the specified selector in the ElementHandles subtree. See Working with selectors for more details. If no elements match the selector, returns empty array.


[View source]
abstract def screenshot(options : ScreenshotOptions | Nil) : Bytes #

Returns the buffer with the captured 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.


[View source]
def screenshot : Bytes #

[View source]
abstract def scroll_into_view_if_needed(options : ScrollIntoViewIfNeededOptions | Nil) : Nil #

This method waits for actionability checks, then tries to scroll element into view, unless it is completely visible as defined by IntersectionObserver's ratio. Throws when elementHandle does not point to an element connected to a Document or a ShadowRoot.


[View source]
def scroll_into_view_if_needed : Nil #

[View source]
def select_option(value : String, options : SelectOptionOptions | Nil) #

[View source]
def select_option(values : Array(String), options : SelectOptionOptions | Nil) #

[View source]
def select_option(value : SelectOption | Nil, options : SelectOptionOptions | Nil) #

[View source]
abstract def select_option(values : Array(SelectOption) | Nil, options : SelectOptionOptions | Nil) #

[View source]
def select_option(value : ElementHandle | Nil, options : SelectOptionOptions | Nil) #

[View source]
abstract def select_option(values : Array(ElementHandle) | Nil, options : SelectOptionOptions | Nil) #

[View source]
def select_option(value : String) #

[View source]
def select_option(values : Array(String)) #

[View source]
def select_option(value : SelectOption | Nil) #

[View source]
def select_option(values : Array(SelectOption) | Nil) #

[View source]
def select_option(value : ElementHandle | Nil) #

[View source]
def select_option(values : Array(ElementHandle) | Nil) #

[View source]
abstract def select_text(options : SelectTextOptions | Nil) : Nil #

This method waits for actionability checks, then focuses the element and selects all its text content.


[View source]
def select_text : Nil #

[View source]
abstract def set_input_files(file : Array(FileChooser::FilePayload), options : SetInputFilesOptions | Nil) #

This method expects elementHandle 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.


[View source]
def set_input_files(file : Path, options : SetInputFilesOptions | Nil) #

[View source]
abstract def set_input_files(file : Array(Path), options : SetInputFilesOptions | Nil) #

[View source]
def set_input_files(file : FileChooser::FilePayload, options : SetInputFilesOptions | Nil) #

[View source]
def set_input_files(files : Array(FileChooser::FilePayload)) #

[View source]
def set_input_files(file : Path) #

[View source]
def set_input_files(files : Array(Path)) #

[View source]
def set_input_files(file : FileChooser::FilePayload) #

[View source]
abstract def tap(options : TapOptions | Nil) : Nil #

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 in the center of the element, or the specified position. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.

If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified timeout, this method rejects with a TimeoutError. Passing zero timeout disables this.

NOTE elementHandle.tap() requires that the hasTouch option of the browser context be set to true.


[View source]
def tap : Nil #

[View source]
abstract def text_content : String | Nil #

Returns the node.textContent.


[View source]
abstract def to_string : String #

[View source]
abstract def type(text : String, options : TypeOptions | Nil) : Nil #

Focuses the element, and then sends a keydown, keypress/input, and keyup event for each character in the text. To press a special key, like Control or ArrowDown, use elementHandle.press(key[, options]).

An example of typing into a text field and then submitting the form:


[View source]
def type(text : String) : Nil #

[View source]
abstract def uncheck(options : UncheckOptions | Nil) : Nil #

This method checks the element by performing the following steps:

Ensure that element is a checkbox or a radio input. If not, this method rejects. If the element is already unchecked, this method returns immediately. Wait for actionability checks on the element, unless force option is set. Scroll the element into view if needed. Use page.mouse to click in the center of the element. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set. Ensure that the element is now unchecked. If not, this method rejects.

If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified timeout, this method rejects with a TimeoutError. Passing zero timeout disables this.


[View source]
def uncheck : Nil #

[View source]
abstract def wait_for_element_state(state : ElementState, options : WaitForElementStateOptions | Nil) : Deferred(Nil) #

Returns the element satisfies the state. Depending on the state parameter, this method waits for one of the actionability checks to pass. This method throws when the element is detached while waiting, unless waiting for the "hidden" state.

"visible" Wait until the element is visible. "hidden" Wait until the element is not visible or not attached. Note that waiting for hidden does not throw when the element detaches. "stable" Wait until the element is both visible and stable. "enabled" Wait until the element is enabled. "disabled" Wait until the element is not enabled.

If the element does not satisfy the condition for the timeout milliseconds, this method will throw.


[View source]
def wait_for_element_state(state : ElementState) : Deferred(Nil) #

[View source]
abstract def wait_for_selector(selector : String, options : WaitForSelectorOptions | Nil) : Deferred(ElementHandle | Nil) #

Returns element specified by selector satisfies state option. Returns null if waiting for hidden or detached. Wait for the selector relative to the element handle to satisfy state option (either appear/disappear from dom, or become visible/hidden). If at the moment of calling the method selector already satisfies the condition, the method will return immediately. If the selector doesn't satisfy the condition for the timeout milliseconds, the function will throw.

NOTE This method does not work across navigations, use page.waitForSelector(selector[, options]) instead.


[View source]
def wait_for_selector(selector : String) : Deferred(ElementHandle | Nil) #

[View source]