Playwright.Locator (playwright v0.1.17-preview-1) View Source

Playwright.Locator represents a view to the element(s) on the page. It captures the logic sufficient to retrieve the element at any given moment. Locator can be created with the Playwright.Locator.new/2 function.

Example

locator = Page.Locator.new(page, "a#exists")
Locator.click(locator)

The difference between the Locator and Playwright.ElementHandle is that the latter points to a particular element, while Locator captures the logic of how to retrieve that element.

ElementHandle Example

In the example below, handle points to a particular DOM element on page. If that element changes text or is used by React to render an entirely different component, handle is still pointing to that very DOM element. This can lead to unexpected behaviors.

{:ok, handle} = Page.query_selector(page, "text=Submit")
ElementHandle.hover(handle)
ElementHandle.click(handle)

Locator Example

With the locator, every time the element is used, up-to-date DOM element is located in the page using the selector. So in the snippet below, underlying DOM element is going to be located twice.

locator = Page.Locator.new(page, "a#exists")
:ok = Page.Locator.hover(locator)
:ok = Page.Locator.click(locator)

Strictness

Locators are strict. This means that all operations on locators that imply some target DOM element will throw if more than one element matches given selector.

alias Page.Locator
locator = Locator.new(page, "button")

# Throws if there are several buttons in DOM:
Locator.click(locator)

# Works because we explicitly tell locator to pick the first element:
Locator.first(locator) |> Locator.click()

# Works because count knows what to do with multiple matches:
Locator.count(locator)

Link to this section Summary

Functions

Checks the (checkmark) element by performing the following steps

Clicks the element by performing the following steps

Double clicks the element.

Dispatches the param: type event on the element.

Resolves the given Playwright.Locator to the first matching DOM element.

Resolves given locator to all matching DOM elements.

Fills a form field or contenteditable element with text.

Calls focus on the element.

Hovers over the element.

Returns a %Playwright.Locator{}.

Selects one or more options from a <select> element.

Unchecks the (checkmark) element by performing the following steps

Returns when element specified by locator satisfies option: state.

Link to this section Types

Specs

options() :: %{optional(:timeout) => non_neg_integer()}

Specs

options_click() :: %{
  optional(:button) => param_input_button(),
  optional(:click_count) => number(),
  optional(:delay) => number(),
  optional(:force) => boolean(),
  optional(:modifiers) => [:alt | :control | :meta | :shift],
  optional(:no_wait_after) => boolean(),
  optional(:position) => options_position(),
  optional(:timeout) => number(),
  optional(:trial) => boolean()
}

Specs

options_position() :: %{optional(:x) => number(), optional(:y) => number()}

Specs

param_input_button() :: :left | :right | :middle

Specs

selector() :: String.t()

Specs

serializable() :: any()

Specs

t() :: %Playwright.Locator{frame: Playwright.Frame.t(), selector: selector()}

Link to this section Functions

Link to this function

all_inner_texts(locator)

View Source

Specs

all_inner_texts(t()) :: {:ok, [binary()]}
Link to this function

all_text_contents(locator)

View Source

Specs

all_text_contents(t()) :: {:ok, [binary()]}
Link to this function

check(locator, options \\ %{})

View Source

Specs

check(t(), options()) :: :ok | {:error, Playwright.Runner.Channel.Error.t()}

Checks the (checkmark) element by performing the following steps:

  1. Ensure that element is a checkbox or a radio input. If not, this method throws. If the element is already checked, this method returns immediately.
  2. Wait for actionability checks on the element, unless force option is set.
  3. Scroll the element into view if needed.
  4. Use Playwright.Page.Mouse to click in the center of the element.
  5. Wait for initiated navigations to either succeed or fail, unless option: no_wait_after is set.
  6. Ensure that the element is now checked. If not, this method throws.

If the element is detached from the DOM at any moment during the action, this method throws.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing 0 timeout disables this.

Returns

  • :ok

Arguments

key / nametypedescription
:forceoptionboolean()Whether to bypass the actionability checks. (default: false)
:no_wait_afteroptionboolean()Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. (default: false)
:positionoption%{x: x, y: y}A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed via Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2. (default: 30 seconds)
:trialoptionboolean()When set, this call only performs the actionability checks and skips the action. Useful to wait until the element is ready for the action without performing it. (default: false)
Link to this function

click(locator, options \\ %{})

View Source

Specs

click(t(), options_click()) ::
  :ok | {:error, Playwright.Runner.Channel.Error.t()}

Clicks the element by performing the following steps:

  1. Wait for actionability checks on the element, unless option: force is set.
  2. Scroll the element into view if needed.
  3. Use Playwright.Page.Mouse to click in the center of the element, or the specified position.
  4. Wait for initiated navigations to either succeed or fail, unless option: no_wait_after is set.

If the element is detached from the DOM at any moment during the action, this method throws.

When all steps combined have not finished during the specified timeout, this method throws a Playwright.Runner.Channel.Error.t(). Passing 0 timeout disables this.

Returns

  • :ok

Arguments

key / nametypedescription
:buttonoption:left, :right or :middle(default: :left)
:click_countoptionnumber()See MDN: UIEvent.detail (default: 1)
:delayoptionnumber()Time to wait between mousedown and mouseup in milliseconds. (default: 0)
:forceoptionboolean()Whether to bypass the actionability checks. (default: false)
:modifiersoption[:alt, :control, :meta, :shift]Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used.
:no_wait_afteroptionboolean()Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. (default: false)
:positionoption%{x: x, y: y}A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed via Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2. (default: 30 seconds)
:trialoptionboolean()When set, this call only performs the actionability checks and skips the action. Useful to wait until the element is ready for the action without performing it. (default: false)
Link to this function

dblclick(locator, options \\ %{})

View Source

Specs

dblclick(t(), options()) :: :ok

Double clicks the element.

Performs the following steps:

  1. Wait for actionability checks on the matched element, unless option: force is set.
  2. Scroll the element into view if needed. 3 Use Page.Mouse to double click in the center of the element, or the specified option: position.
  3. Wait for initiated navigations to either succeed or fail, unless option: no_wait_after is set. Note that if the first click of the dblclick/3 triggers a navigation event, the call will throw.

If the element is detached from the DOM at any moment during the action, the call throws.

When all steps combined have not finished during the specified option: timeout, throws a TimeoutError. Passing timeout: 0 disables this.

NOTE

dblclick/3 dispatches two click events and a single dblclick event.

Returns

  • :ok

Arguments

key / nametypedescription
:buttonoption:left, :right or :middle(default: :left)
:delayoptionnumber()Time to wait between keydown and keyup in milliseconds. (default: 0)
:forceoptionboolean()Whether to bypass the actionability checks. (default: false)
:modifiersoption[:alt, :control, :meta, :shift]Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used.
:no_wait_afteroptionboolean()Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. (default: false)
:positionoption%{x: x, y: y}A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed by using the Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2 functions. (default: 30 seconds)
:trialoptionboolean()When set, this call only performs the actionability checks and skips the action. Useful to wait until the element is ready for the action without performing it. (default: false)
Link to this function

dispatch_event(locator, type, event_init \\ nil, options \\ %{})

View Source

Specs

dispatch_event(
  t(),
  atom() | binary(),
  Playwright.Frame.evaluation_argument(),
  options()
) :: :ok

Dispatches the param: type event on the element.

Regardless of the visibility state of the element, the event is dispatched.

Under the hood, creates an instance of an event based on the given type, initializes it with the param: event_init properties and dispatches it on the element.

Events are composed, cancelable and bubble by default.

The param: event_init is event-specific. Please refer to the events documentation for the lists of initial properties:

Example

Dispatch a 'click' event on the element. This is equivalent to calling Playwright.ElementHandle.click/2:

Locator.dispatch_event(locator, :click)

Specify a Playwright.JSHandle as the property value to be passed into the event:

data_transfer = Page.evaluate_handle(page, "new DataTransfer()")
Locator.dispatch_event(locator, :dragstart, { "dataTransfer": data_transfer })

Returns

  • :ok

Arguments

key / nametypedescription
typeparamatom() or binary()DOM event type: :click, :dragstart, etc.
event_initparamevaluation_argument()Optional event-specific initialization properties.
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed by using the Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2 functions. (default: 30 seconds)
Link to this function

element_handle(locator, options \\ %{})

View Source

Specs

element_handle(t(), options()) ::
  {:ok, Playwright.ElementHandle.t()}
  | {:error, Playwright.Runner.Channel.Error.t()}

Resolves the given Playwright.Locator to the first matching DOM element.

If no elements matching the query are visible, waits for them up to a given timeout. If multiple elements match the selector, throws.

Returns

  • {:ok, Playwright.ElementHandle.t()}
  • {:error, Playwright.Runner.Channel.Error.t()}

Arguments

key / nametypedescription
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed by using the Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2 functions. (default: 30 seconds)
Link to this function

element_handles(locator)

View Source

Specs

element_handles(t()) :: {:ok, [Playwright.ElementHandle.t()]}

Resolves given locator to all matching DOM elements.

Returns

  • [Playwright.ElementHandle.t()]
Link to this function

evaluate(locator, expression, arg \\ nil, options \\ %{})

View Source

Specs

evaluate(t(), binary(), Playwright.ElementHandle.t() | nil, options()) ::
  {:ok, serializable()}

...

Link to this function

evaluate_all(locator, expression, arg \\ nil)

View Source

Specs

evaluate_all(t(), binary(), Playwright.ElementHandle.t() | nil) ::
  {:ok, [serializable()]}

...

Link to this function

evaluate_handle(locator, expression, arg \\ nil, options \\ %{})

View Source

Specs

evaluate_handle(t(), binary(), any(), options()) ::
  {:ok, Playwright.JSHandle.t()} | {:error, Playwright.Runner.Channel.Error.t()}

Returns the result of param: expression as a Playwright.JSHandle.

Passes the handle as the first argument to param: expression.

The only difference between Playwright.Locator.evaluate/4 and Playwright.Locator.evaluate_handle/3 is that evaluate_handle returns JSHandle.

See Playwright.Page.evaluate_handle for more details.

Returns

  • {:ok, Playwright.JSHandle.t()}
  • {:error, Playwright.Runner.Channel.Error.t()}

Arguments

key / nametypedescription
expressionparambinary()JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted as a function. Otherwise, evaluated as an expression.
argparamany()Argument to pass to expression (optional)
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed by using the Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2 functions. (default: 30 seconds)
Link to this function

fill(locator, value, options \\ %{})

View Source

Specs

fill(t(), binary(), options()) :: :ok

Fills a form field or contenteditable element with text.

Waits for an element matching param: selector, waits for "actionability checks", focuses the element, fills it and triggers an input event after filling.

If the target element is not an <input>, <textarea> or contenteditable element, this function raises an error. However, if the element is inside the <label> element that has an associated control, the control will be filled instead.

NOTE

  • Pass an empty string to clear the input field.
  • To send fine-grained keyboard events, use Playwright.Locator.type/3.

Returns

  • :ok

Arguments

key / nametypedescription
valueparambinary()Value to fill for the <input>, <textarea> or [contenteditable] element
:forceoptionboolean()Whether to bypass the actionability checks. (default: false)
:no_wait_afteroptionboolean()Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. (default: false)
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed by using the Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2 functions. (default: 30 seconds)
Link to this function

focus(locator, options \\ %{})

View Source

Specs

focus(t(), options()) :: :ok

Calls focus on the element.

Returns

  • :ok

Arguments

key / nametypedescription
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed by using the Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2 functions. (default: 30 seconds)
Link to this function

get_attribute(locator, name, options \\ %{})

View Source

Specs

get_attribute(t(), binary(), options()) :: {:ok, binary() | nil}
Link to this function

hover(locator, options \\ %{})

View Source

Specs

hover(t(), options()) :: :ok

Hovers over the element.

Performs the following steps:

  1. Wait for actionability checks on the matched element, unless option: force option is set. If the element is detached during the checks, the whole action is retried.
  2. Scroll the element into view if needed.
  3. Use Page.Mouse to hover over the center of the element, or the specified option: position.
  4. Wait for initiated navigations to either succeed or fail, unless option: no_wait_after is set.

When all steps combined have not finished during the specified option: timeout, throws a TimeoutError. Passing 0 timeout disables this.

Returns

  • :ok

Arguments

key / nametypedescription
selectorparambinary()A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. See "working with selectors (guide)" for more details.
:forceoptionboolean()Whether to bypass the actionability checks. (default: false)
:modifiersoption[:alt, :control, :meta, :shift]Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used.
:no_wait_afteroptionboolean()Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. (default: false)
:positionoption%{x: x, y: y}A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed by using the Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2 functions. (default: 30 seconds)
:trialoptionboolean()When set, this call only performs the actionability checks and skips the action. Useful to wait until the element is ready for the action without performing it. (default: false)
Link to this function

inner_html(locator, options \\ %{})

View Source

Specs

inner_html(t(), options()) :: {:ok, binary()}
Link to this function

inner_text(locator, options \\ %{})

View Source

Specs

inner_text(t(), options()) :: {:ok, binary()}
Link to this function

input_value(locator, options \\ %{})

View Source

Specs

input_value(t(), options()) :: {:ok, binary()}
Link to this function

is_checked(locator, options \\ %{})

View Source

Specs

is_checked(t(), options()) ::
  :ok | {:error, Playwright.Runner.Channel.Error.t()}
Link to this function

is_disabled(locator, options \\ %{})

View Source

Specs

is_disabled(t(), options()) ::
  :ok | {:error, Playwright.Runner.Channel.Error.t()}
Link to this function

is_editable(locator, options \\ %{})

View Source

Specs

is_editable(t(), options()) ::
  :ok | {:error, Playwright.Runner.Channel.Error.t()}
Link to this function

is_enabled(locator, options \\ %{})

View Source

Specs

is_enabled(t(), options()) ::
  :ok | {:error, Playwright.Runner.Channel.Error.t()}
Link to this function

is_hidden(locator, options \\ %{})

View Source

Specs

is_hidden(t(), options()) :: :ok | {:error, Playwright.Runner.Channel.Error.t()}
Link to this function

is_visible(locator, options \\ %{})

View Source

Specs

is_visible(t(), options()) ::
  :ok | {:error, Playwright.Runner.Channel.Error.t()}
Link to this function

locator(locator, selector)

View Source

Specs

locator(t(), binary()) :: t()

Specs

Returns a %Playwright.Locator{}.

Arguments

key / nametypedescription

| frame | param | Frame.t() | Page.t() | | | selector | param | binary() | A Playwright selector. |

Link to this function

select_option(locator, values, options \\ %{})

View Source

Specs

select_option(t(), any(), options()) :: {:ok, [binary()]}

Selects one or more options from a <select> element.

Performs the following steps:

  1. Waits for actionability checks
  2. Waits until all specified options are present in the <select> element
  3. Selects those options

If the target element is not a <select> element, raises an error. However, if the element is inside the <label> element that has an associated control, the control will be used instead.

Returns the list of option values that have been successfully selected.

Triggers a change and input event once all the provided options have been selected.

Example

locator = Page.locator(page, "select#colors")

# single selection matching the value
Locator.select_option(locator, "blue")

# single selection matching both the label
Locator.select_option(locator, %{label: "blue"})

# multiple selection
Locator.select_option(locator, %{value: ["red", "green", "blue"]})

Returns

  • {:ok, [binary()]}

Arguments

key / nametypedescription
valuesparamany()Options to select.
:forceoptionboolean()Whether to bypass the actionability checks. (default: false)
:no_wait_afteroptionboolean()Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. (default: false)
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed by using the Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2 functions. (default: 30 seconds)

On values

If the <select> has the multiple attribute, all matching options are selected, otherwise only the first option matching one of the passed options is selected.

String values are equivalent to %{value: "string"}.

Option is considered matching if all specified properties match.

  • value <binary> Matches by option.value. (optional).
  • label <binary> Matches by option.label. (optional).
  • index <number> Matches by the index. (optional).
Link to this function

text_content(locator, options \\ %{})

View Source

Specs

text_content(t(), options()) ::
  :ok | {:error, Playwright.Runner.Channel.Error.t()}
Link to this function

uncheck(locator, options \\ %{})

View Source

Specs

uncheck(t(), options()) :: :ok | {:error, Playwright.Runner.Channel.Error.t()}

Unchecks the (checkmark) element by performing the following steps:

  1. Ensure that element is a checkbox or a radio input. If not, this method throws. If the element is already checked, this method returns immediately.
  2. Wait for actionability checks on the element, unless force option is set.
  3. Scroll the element into view if needed.
  4. Use Playwright.Page.Mouse to click in the center of the element.
  5. Wait for initiated navigations to either succeed or fail, unless option: no_wait_after is set.
  6. Ensure that the element is now checked. If not, this method throws.

If the element is detached from the DOM at any moment during the action, this method throws.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing 0 timeout disables this.

Returns

  • :ok

Arguments

key / nametypedescription
:forceoptionboolean()Whether to bypass the actionability checks. (default: false)
:no_wait_afteroptionboolean()Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. (default: false)
:positionoption%{x: x, y: y}A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed via Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2. (default: 30 seconds)
:trialoptionboolean()When set, this call only performs the actionability checks and skips the action. Useful to wait until the element is ready for the action without performing it. (default: false)
Link to this function

wait_for(locator, options \\ %{})

View Source

Specs

wait_for(t(), options()) :: :ok | {:error, Playwright.Runner.Channel.Error.t()}

Returns when element specified by locator satisfies option: state.

If target element already satisfies the condition, the method returns immediately. Otherwise, waits for up to option: timeout milliseconds until the condition is met.