Playwright.ElementHandle (playwright v0.1.17-preview-7) View Source
ElementHandle represents an in-page DOM element.
ElementHandles can be created with the Playwright.Page.query_selector/3
function, and similar.
⚠️ DISCOURAGED
The use of
Playwright.ElementHandleis discouraged; usePlaywright.Locatorinstances and web-first assertions instead.
Example
handle = Page.q(page, "a")
:ok = ElementHandle.click(handle)ElementHandle prevents DOM elements from garbage collection unless the
handle is disposed with Playwright.JSHandle.dispose/1. ElementHandles
are auto-disposed when their origin frame is navigated.
An ElementHandle instance can be used as an argument in
Playwright.Page.eval_on_selector/5 and Playwright.Page.evaluate/3.
NOTE
In most cases, you would want to use
Playwright.Locatorinstead. You should only useElementHandleif you want to retain a handle to a particular DOM node that you intend to pass intoPlaywright.Page.evaluate/3as an argument.
The difference between Playwright.Locator and ElementHandle is that
ElementHandle points to a particular element, while Playwright.Locator
captures the logic of how to retrieve an element.
In the example below, handle points to a particular DOM element on the
page. If that element changes text or is used by JavaScript to render an
entirely different component, handle still points to that very DOM element.
This can lead to unexpected behaviors.
handle = Page.q("text=Submit")
ElementHandle.hover(handle)
ElementHandle.click(handle)With the Playwright.Locator, every time the locator is used, an
up-to-date DOM element is located in the page using the selector. So, in the
snippet below, the underlying DOM element is going to be located twice.
locator = Page.locator("text=Submit")
Locator.hover(locator)
Locator.click(locator)
Link to this section Summary
Functions
Clicks on the element, performing the following steps
Returns the Playwright.Frame for element handles referencing iframe nodes,
or nil otherwise.
Returns the value of an element's attribute.
Optional callback implementation for Playwright.ChannelOwner.init/2.
Searches within an element for a DOM element matching the given selector.
Returns the node.textContent (all text within the element).
Link to this section Types
Link to this section Functions
Specs
Specs
Clicks on the element, performing the following steps:
- Wait for "actionability (guide)" checks on the element, unless
force: trueoption is set. - Scroll the element into view, if needed.
- Use
Playwright.Page.Mouseto click the center of the elemnt, or the specified option:position. - Wait for initiated navigations to either succeed or fail, unless
no_wait_after: trueoption is set.
If the element is detached from the DOM at any moment during the action, this function raises.
When all steps combined have not finished during the specified :timeout,
this function raises a TimeoutError. Passing zero (0) for timeout
disables this.
Specs
content_frame(t()) :: Playwright.Frame.t() | nil
Returns the Playwright.Frame for element handles referencing iframe nodes,
or nil otherwise.
Specs
Returns the value of an element's attribute.
Specs
Optional callback implementation for Playwright.ChannelOwner.init/2.
If implemented, the callback will receive:
- The newly created "channel owner" struct.
- The
:initializerreceived from the Playwright browser server.
The implementation has the option of "patching" the struct as stored in the catalog, and/or binding event handlers.
Example
def init(%{session: session} = owner, _initializer) do
Channel.bind(session, {:guid, owner.guid}, :close, fn event ->
Logger.warn("Closing #{inspect(event.target)}")
end)
{:ok, %{owner | version: "1.2.3"}}
endReturns
{:ok, struct()}
Arguments
| key/name | type | description | |
|---|---|---|---|
owner | param | struct() | The newly created channel owner (resource). |
initializer | param | struct() | The initializer received from with the channel owner instance was derived. |
Specs
Specs
Searches within an element for a DOM element matching the given selector.
Finds an element matching the specified selector within the subtree of the
ElementHandle. See "working with selectors (guide)" for more details.
If no elements match the selector, returns nil.
Specs
Specs
Specs
Specs
Returns the node.textContent (all text within the element).