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.crInstance Method Summary
-
#bounding_box : BoundingBox | Nil
This method returns the bounding box of the element, or
null
if the element is not visible. -
#check(options : CheckOptions | Nil) : Nil
This method checks the element by performing the following steps:
- #check : Nil
-
#click(options : ClickOptions | Nil) : Nil
This method clicks the element by performing the following steps:
- #click : Nil
-
#content_frame : Frame | Nil
Returns the content frame for element handles referencing iframe nodes, or
null
otherwise -
#dblclick(options : DblclickOptions | Nil) : Nil
This method double clicks the element by performing the following steps:
- #dblclick : Nil
-
#dispatch_event(type : String, event_init : Array(Any) | Nil) : Nil
The snippet below dispatches the
#click
event on the element. - #dispatch_event(type : String) : Nil
- #dispatch_event(type : String, *event_init : Any) : Nil
-
#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 theElementHandle
s subtree and passes it as a first argument topageFunction
. - #eval_on_selector(selector : String, page_function : String) : Any
- #eval_on_selector(selector : String, page_function : String, *arg : Any) : Any
-
#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 theElementHandle
's subtree and passes an array of matched elements as a first argument topageFunction
. - #eval_on_selector_all(selector : String, page_function : String) : Any
- #eval_on_selector_all(selector : String, page_function : String, *arg : Any) : Any
-
#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. - #fill(value : String) : Nil
-
#focus : Nil
Calls focus on the element.
-
#get_attribute(name : String) : String | Nil
Returns element attribute value.
-
#hover(options : HoverOptions | Nil) : Nil
This method hovers over the element by performing the following steps:
- #hover : Nil
-
#inner_html : String
Returns the
element.innerHTML
. -
#inner_text : String
Returns the
element.innerText
. -
#owner_frame : Frame | Nil
Returns the frame containing the given element.
-
#press(key : String, options : PressOptions | Nil) : Nil
Focuses the element, and then uses
keyboard.down(key)
andkeyboard.up(key)
. - #press(key : String) : Nil
-
#query_selector(selector : String) : ElementHandle | Nil
The method finds an element matching the specified selector in the
ElementHandle
's subtree. -
#query_selector_all(selector : String) : Array(ElementHandle)
The method finds all elements matching the specified selector in the
ElementHandle
s subtree. -
#screenshot(options : ScreenshotOptions | Nil) : Bytes
Returns the buffer with the captured screenshot.
- #screenshot : Bytes
-
#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
. - #scroll_into_view_if_needed : Nil
- #select_option(value : String, options : SelectOptionOptions | Nil)
- #select_option(values : Array(String), options : SelectOptionOptions | Nil)
- #select_option(value : SelectOption | Nil, options : SelectOptionOptions | Nil)
- #select_option(values : Array(SelectOption) | Nil, options : SelectOptionOptions | Nil)
- #select_option(value : ElementHandle | Nil, options : SelectOptionOptions | Nil)
- #select_option(values : Array(ElementHandle) | Nil, options : SelectOptionOptions | Nil)
- #select_option(value : String)
- #select_option(values : Array(String))
- #select_option(value : SelectOption | Nil)
- #select_option(values : Array(SelectOption) | Nil)
- #select_option(value : ElementHandle | Nil)
- #select_option(values : Array(ElementHandle) | Nil)
-
#select_text(options : SelectTextOptions | Nil) : Nil
This method waits for actionability checks, then focuses the element and selects all its text content.
- #select_text : Nil
-
#set_input_files(file : Array(FileChooser::FilePayload), options : SetInputFilesOptions | Nil)
This method expects
elementHandle
to point to an input element. - #set_input_files(file : Path, options : SetInputFilesOptions | Nil)
- #set_input_files(file : Array(Path), options : SetInputFilesOptions | Nil)
- #set_input_files(file : FileChooser::FilePayload, options : SetInputFilesOptions | Nil)
- #set_input_files(files : Array(FileChooser::FilePayload))
- #set_input_files(file : Path)
- #set_input_files(files : Array(Path))
- #set_input_files(file : FileChooser::FilePayload)
-
#tap(options : TapOptions | Nil) : Nil
This method taps the element by performing the following steps:
- #tap : Nil
-
#text_content : String | Nil
Returns the
node.textContent
. - #to_string : String
-
#type(text : String, options : TypeOptions | Nil) : Nil
Focuses the element, and then sends a
keydown
,keypress
/input
, andkeyup
event for each character in the text. - #type(text : String) : Nil
-
#uncheck(options : UncheckOptions | Nil) : Nil
This method checks the element by performing the following steps:
- #uncheck : Nil
-
#wait_for_element_state(state : ElementState, options : WaitForElementStateOptions | Nil) : Deferred(Nil)
Returns the element satisfies the
state
. - #wait_for_element_state(state : ElementState) : Deferred(Nil)
-
#wait_for_selector(selector : String, options : WaitForSelectorOptions | Nil) : Deferred(ElementHandle | Nil)
Returns element specified by selector satisfies
state
option. - #wait_for_selector(selector : String) : Deferred(ElementHandle | Nil)
Instance methods inherited from module Playwright::JSHandle
as_element : ElementHandle | Nil
as_element,
dispose : Nil
dispose,
evaluate(page_function : String, arg : Array(Any) | Nil) : Anyevaluate(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
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.
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.
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.
Returns the content frame for element handles referencing iframe nodes, or null
otherwise
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.
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:
Returns the return value of pageFunction
The method finds an element matching the specified selector in the ElementHandle
s 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:
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:
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.
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.
Returns the frame containing the given element.
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.
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
.
The method finds all elements matching the specified selector in the ElementHandle
s subtree. See Working with selectors for more details. If no elements match the selector, returns empty array.
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.
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.
This method waits for actionability checks, then focuses the element and selects all its text content.
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.
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.
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:
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.
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.
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.