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? as_element, dispose : Nil dispose, evaluate(page_function : String, arg : Array(Any)?) : Any
evaluate(page_function : String) : Any
evaluate(page_function : String, *arg : Any) : Any
evaluate
, evaluate_handle(page_function : String, arg : Array(Any)?) : 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? #

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 #

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]
def click : Nil #

[View source]
abstract def click(options : ClickOptions?) : 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]
abstract def content_frame : Frame? #

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


[View source]
def dblclick : Nil #

[View source]
abstract def dblclick(options : DblclickOptions?) : 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 dispatch_event(type : String, *event_init : Any) : Nil #

[View source]
abstract def dispatch_event(type : String, event_init : Array(Any)?) : 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 eval_on_selector(selector : String, page_function : String, *arg : Any) : Any #

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

[View source]
abstract def eval_on_selector(selector : String, page_function : String, arg : Array(Any)?) : 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_all(selector : String, page_function : String, *arg : Any) : Any #

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

[View source]
abstract def eval_on_selector_all(selector : String, page_function : String, arg : Array(Any)?) : 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]
abstract def fill(value : String, options : FillOptions?) : 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 ,