module

Playwright::ElementHandle

Inherits Playwright::JSHandle

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.

Instance methods

bounding_box

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.

Source
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.

Source
check
Source
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.

Source
click
Source
content_frame

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

Source
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.

Source
dblclick
Source
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:

Source
dispatch_event(type : String) : Nil
Source
dispatch_event(type : String, *event_init : Any) : Nil
Source
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:

Source
eval_on_selector(selector : String, page_function : String) : Any
Source
eval_on_selector(selector : String, page_function : String, *arg : Any) : Any
Source
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:

Source
eval_on_selector_all(selector : String, page_function : String) : Any
Source
eval_on_selector_all(selector : String, page_function : String, *arg : Any) : Any
Source
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.

Source
fill(value : String) : Nil
Source
focus

Calls focus on the element.

Source
get_attribute(name : String) : String | Nil

Returns element attribute value.

Source
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.

Source
hover
Source
inner_html

Returns the element.innerHTML.

Source
inner_text

Returns the element.innerText.

Source
owner_frame

Returns the frame containing the given element.

Source
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.

Source
press(key : String) : Nil
Source
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.

Source
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.

Source
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.

Source
screenshot
Source
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.

Source
scroll_into_view_if_needed
Source
select_option(value : String, options : SelectOptionOptions | Nil)
Source
select_option(values : Array(String), options : SelectOptionOptions | Nil)
Source
select_option(value : SelectOption | Nil, options : SelectOptionOptions | Nil)
Source
select_option(values : Array(SelectOption) | Nil, options : SelectOptionOptions | Nil)
Source
select_option(value : ElementHandle | Nil, options : SelectOptionOptions | Nil)
Source
select_option(values : Array(ElementHandle) | Nil, options : SelectOptionOptions | Nil)
Source
select_option(value : String)
Source
select_option(values : Array(String))
Source
select_option(value : SelectOption | Nil)
Source
select_option(values : Array(SelectOption) | Nil)
Source
select_option(value : ElementHandle | Nil)
Source
select_option(values : Array(ElementHandle) | Nil)
Source
select_text(options : SelectTextOptions | Nil) : Nil

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

Source
select_text
Source
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.

Source
set_input_files(file : Path, options : SetInputFilesOptions | Nil)
Source
set_input_files(file : Array(Path), options : SetInputFilesOptions | Nil)
Source
set_input_files(file : FileChooser::FilePayload, options : SetInputFilesOptions | Nil)
Source
set_input_files(files : Array(FileChooser::FilePayload))
Source
set_input_files(file : Path)
Source
set_input_files(files : Array(Path))
Source
set_input_files(file : FileChooser::FilePayload)
Source
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.

Source
text_content

Returns the node.textContent.

Source
to_string
Source
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:

Source
type(text : String) : Nil
Source
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.

Source
uncheck
Source
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.

Source
wait_for_element_state(state : ElementState) : Deferred(Nil)
Source
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.

Source
wait_for_selector(selector : String) : Deferred(ElementHandle | Nil)
Source

Nested types