Playwright.Locator (Playwright v1.65.0-rc.3)

Copy Markdown View Source

Locators are the central piece of Playwright's auto-waiting and retry-ability. In a nutshell, locators represent a way to find element(s) on the page at any moment. A locator can be created with the Playwright.Page.locator/3 method.

Learn more about locators.

Since Playwright 1.14.

The examples alias the modules they use (alias Playwright.{Locator, Page}).

Summary

Types

Options of aria_snapshot/2, see its docs.

Options of blur/2, see its docs.

Options of bounding_box/2, see its docs.

Options of check/2, see its docs.

Options of clear/2, see its docs.

Options of click/2, see its docs.

Options of dblclick/2, see its docs.

Options of dispatch_event/4, see its docs.

Options of drag_to/3, see its docs.

Options of drop/3, see its docs.

Options of element_handle/2, see its docs.

Options of evaluate_handle/4, see its docs.

Options of evaluate/4, see its docs.

Options of fill/3, see its docs.

Options of filter/2, see its docs.

Options of focus/2, see its docs.

Options of get_attribute/3, see its docs.

Options of get_by_alt_text/3, see its docs.

Options of get_by_label/3, see its docs.

Options of get_by_role/3, see its docs.

Options of get_by_text/3, see its docs.

Options of get_by_title/3, see its docs.

Options of highlight/2, see its docs.

Options of hover/2, see its docs.

Options of inner_html/2, see its docs.

Options of inner_text/2, see its docs.

Options of input_value/2, see its docs.

Options of is_checked/2, see its docs.

Options of is_disabled/2, see its docs.

Options of is_editable/2, see its docs.

Options of is_enabled/2, see its docs.

Options of is_hidden/2, see its docs.

Options of is_visible/2, see its docs.

Options of locator/3, see its docs.

Options of press/3, see its docs.

Options of screenshot/2, see its docs.

Options of select_option/3, see its docs.

Options of select_text/2, see its docs.

Options of set_checked/3, see its docs.

Options of set_input_files/3, see its docs.

t()

A Locator handle. Compare handles with ==, their fields are internal.

Options of tap/2, see its docs.

Options of text_content/2, see its docs.

Options of type/3, see its docs.

Options of uncheck/2, see its docs.

Options of wait_for/2, see its docs.

Functions

When the locator points to a list of elements, this returns an array of locators, pointing to their respective elements.

Like all/1, but returns the value and raises on error.

Returns an array of node.innerText values for all matching nodes.

Like all_inner_texts/1, but returns the value and raises on error.

Returns an array of node.textContent values for all matching nodes.

Like all_text_contents/1, but returns the value and raises on error.

Creates a locator that matches both this locator and the argument locator.

Captures the aria snapshot of the given element. Read more about aria snapshots and Playwright.Expect.to_match_aria_snapshot/3 for the corresponding assertion.

Like aria_snapshot/2, but returns the value and raises on error.

Captures the aria snapshot of the given element as a free form JSON object.

Like aria_snapshot_json/2, but returns the value and raises on error.

Calls blur on the element.

Like blur/2, but returns the value and raises on error.

This method returns the bounding box of the element matching the locator, or nil 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.

Like bounding_box/2, but returns the value and raises on error.

Ensure that checkbox or radio element is checked.

Like check/2, but returns the value and raises on error.

Clear the input field.

Like clear/2, but returns the value and raises on error.

Click an element.

Like click/2, but returns the value and raises on error.

Returns a FrameLocator object pointing to the same iframe as this locator.

Returns the number of elements matching the locator.

Like count/1, but returns the value and raises on error.

Double-click an element.

Like dblclick/2, but returns the value and raises on error.

Describes the locator, description is used in the trace viewer and reports. Returns the locator pointing to the same element.

Returns locator description previously set with Playwright.Locator.describe/2. Returns nil if no custom description has been set. Prefer Playwright.Locator.to_string/1 for a human-readable representation, as it uses the description when available.

Programmatically dispatch an event on the matching element.

Drag the source element towards the target element and drop it.

Like drag_to/3, but returns the value and raises on error.

Simulate an external drag-and-drop of files or clipboard-like data onto this locator.

Like drop/3, but returns the value and raises on error.

Resolves given locator to the first matching DOM element. If there are no matching elements, waits for one. If multiple elements match the locator, fails.

Like element_handle/2, but returns the value and raises on error.

Resolves given locator to all matching DOM elements. If there are no matching elements, returns an empty list.

Like element_handles/1, but returns the value and raises on error.

Execute JavaScript code in the page, taking the matching element as an argument.

Like evaluate/4, but returns the value and raises on error.

Execute JavaScript code in the page, taking all matching elements as an argument.

Like evaluate_all/3, but returns the value and raises on error.

Execute JavaScript code in the page, taking the matching element as an argument, and return a JSHandle with the result.

Set a value to the input field.

Like fill/3, but returns the value and raises on error.

This method narrows existing locator according to the options, for example filters by text. It can be chained to filter multiple times.

Returns locator to the first matching element.

Calls focus on the matching element.

Like focus/2, but returns the value and raises on error.

When working with iframes, you can create a frame locator that will enter the iframe and allow locating elements in that iframe

Returns the matching element's attribute value.

Like get_attribute/3, but returns the value and raises on error.

Allows locating elements by their alt text.

Allows locating input elements by the text of the associated <label> or aria-labelledby element, or by the aria-label attribute.

Allows locating input elements by the placeholder text.

Locate element by the test id.

Allows locating elements that contain given text.

Allows locating elements by their title attribute.

Hides the element highlight previously added by Playwright.Locator.highlight/2.

Like hide_highlight/1, but returns the value and raises on error.

Highlight the corresponding element(s) on the screen. Useful for debugging, don't commit the code that uses Playwright.Locator.highlight/2.

Like highlight/2, but returns the value and raises on error.

Hover over the matching element.

Like hover/2, but returns the value and raises on error.

Like inner_html/2, but returns the value and raises on error.

Like inner_text/2, but returns the value and raises on error.

Returns the value for the matching <input> or <textarea> or <select> element.

Like input_value/2, but returns the value and raises on error.

Returns whether the element is checked. Fails if the element is not a checkbox or radio input.

Like is_checked/2, but returns the value and raises on error.

Returns whether the element is disabled, the opposite of enabled.

Like is_disabled/2, but returns the value and raises on error.

Returns whether the element is editable. If the target element is not an <input>, <textarea>, <select>, [contenteditable] and does not have a role allowing [aria-readonly], this method fails.

Like is_editable/2, but returns the value and raises on error.

Returns whether the element is enabled.

Like is_enabled/2, but returns the value and raises on error.

Returns whether the element is hidden, the opposite of visible.

Like is_hidden/2, but returns the value and raises on error.

Returns whether the element is visible.

Like is_visible/2, but returns the value and raises on error.

Returns locator to the last matching element.

The method finds an element matching the specified selector in the locator's subtree. It also accepts filter options, similar to Playwright.Locator.filter/2 method.

Returns a new locator that uses best practices for referencing the matched element, prioritizing test ids, aria roles, and other user-facing attributes over CSS selectors. This is useful for converting implementation-detail selectors into more resilient, human-readable locators.

Like normalize/1, but returns the value and raises on error.

Returns locator to the n-th matching element. It's zero based, nth(0) selects the first element.

Creates a locator matching all elements that match one or both of the two locators.

A page this locator belongs to.

Focuses the matching element and presses a combination of the keys.

Like press/3, but returns the value and raises on error.

Like press_sequentially/3, but returns the value and raises on error.

Take a screenshot of the element matching the locator.

Like screenshot/2, but returns the value and raises on 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.

Selects option or options in <select>.

Like select_option/3, but returns the value and raises on error.

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

Like select_text/2, but returns the value and raises on error.

Set the state of a checkbox or a radio element.

Like set_checked/3, but returns the value and raises on error.

Upload file or multiple files into <input type=file>. For inputs with a [webkitdirectory] attribute, only a single directory path is supported.

Like set_input_files/3, but returns the value and raises on error.

Perform a tap gesture on the element matching the locator. For examples of emulating other gestures by manually dispatching touch events, see the emulating legacy touch events page.

Like tap/2, but returns the value and raises on error.

Like text_content/2, but returns the value and raises on error.

Returns a human-readable representation of the locator, using the Playwright.Locator.description/1 if one exists; otherwise, it generates a string based on the locator's selector.

Focuses the element, and then sends a keydown, keypress/input, and keyup event for each character in the text.

Like type/3, but returns the value and raises on error.

Ensure that checkbox or radio element is unchecked.

Like uncheck/2, but returns the value and raises on error.

Returns a locator that matches only visible elements, ignoring the invisible ones. This is the recommended way to distinguish elements by visibility, as opposed to the :visible CSS pseudo-class.

Returns when element specified by locator satisfies the :state option.

Like wait_for/2, but returns the value and raises on error.

Returns when expression returns a truthy value, called with the matching element as a first argument, and arg as a second argument.

Returns a locator that matches this locator's elements inside each element matched by other_locator.

Types

aria_snapshot_json_options()

@type aria_snapshot_json_options() ::
  [
    boxes: boolean(),
    depth: integer(),
    mode: :ai | :default,
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:boxes) => boolean(),
      optional(:depth) => integer(),
      optional(:mode) => :ai | :default,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of aria_snapshot_json/2, see its docs.

aria_snapshot_options()

@type aria_snapshot_options() ::
  [
    boxes: boolean(),
    depth: integer(),
    mode: :ai | :default,
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:boxes) => boolean(),
      optional(:depth) => integer(),
      optional(:mode) => :ai | :default,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of aria_snapshot/2, see its docs.

blur_options()

@type blur_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of blur/2, see its docs.

bounding_box_options()

@type bounding_box_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of bounding_box/2, see its docs.

check_options()

@type check_options() ::
  [
    force: boolean(),
    no_wait_after: boolean(),
    position: %{x: number(), y: number()},
    scroll: :auto | :none,
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity,
    trial: boolean()
  ]
  | %{
      optional(:force) => boolean(),
      optional(:no_wait_after) => boolean(),
      optional(:position) => %{x: number(), y: number()},
      optional(:scroll) => :auto | :none,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity,
      optional(:trial) => boolean()
    }

Options of check/2, see its docs.

clear_options()

@type clear_options() ::
  [
    force: boolean(),
    no_wait_after: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:force) => boolean(),
      optional(:no_wait_after) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of clear/2, see its docs.

click_options()

@type click_options() ::
  [
    button: :left | :right | :middle,
    click_count: integer(),
    delay: number(),
    force: boolean(),
    modifiers: [:alt | :control | :control_or_meta | :meta | :shift],
    no_wait_after: boolean(),
    position: %{x: number(), y: number()},
    scroll: :auto | :none,
    signal: Playwright.AbortSignal.t(),
    steps: integer(),
    timeout: number() | :infinity,
    trial: boolean()
  ]
  | %{
      optional(:button) => :left | :right | :middle,
      optional(:click_count) => integer(),
      optional(:delay) => number(),
      optional(:force) => boolean(),
      optional(:modifiers) => [
        :alt | :control | :control_or_meta | :meta | :shift
      ],
      optional(:no_wait_after) => boolean(),
      optional(:position) => %{x: number(), y: number()},
      optional(:scroll) => :auto | :none,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:steps) => integer(),
      optional(:timeout) => number() | :infinity,
      optional(:trial) => boolean()
    }

Options of click/2, see its docs.

dblclick_options()

@type dblclick_options() ::
  [
    button: :left | :right | :middle,
    delay: number(),
    force: boolean(),
    modifiers: [:alt | :control | :control_or_meta | :meta | :shift],
    no_wait_after: boolean(),
    position: %{x: number(), y: number()},
    scroll: :auto | :none,
    signal: Playwright.AbortSignal.t(),
    steps: integer(),
    timeout: number() | :infinity,
    trial: boolean()
  ]
  | %{
      optional(:button) => :left | :right | :middle,
      optional(:delay) => number(),
      optional(:force) => boolean(),
      optional(:modifiers) => [
        :alt | :control | :control_or_meta | :meta | :shift
      ],
      optional(:no_wait_after) => boolean(),
      optional(:position) => %{x: number(), y: number()},
      optional(:scroll) => :auto | :none,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:steps) => integer(),
      optional(:timeout) => number() | :infinity,
      optional(:trial) => boolean()
    }

Options of dblclick/2, see its docs.

dispatch_event_options()

@type dispatch_event_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of dispatch_event/4, see its docs.

drag_to_options()

@type drag_to_options() ::
  [
    force: boolean(),
    no_wait_after: boolean(),
    scroll: :auto | :none,
    signal: Playwright.AbortSignal.t(),
    source_position: %{x: number(), y: number()},
    steps: integer(),
    target_position: %{x: number(), y: number()},
    timeout: number() | :infinity,
    trial: boolean()
  ]
  | %{
      optional(:force) => boolean(),
      optional(:no_wait_after) => boolean(),
      optional(:scroll) => :auto | :none,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:source_position) => %{x: number(), y: number()},
      optional(:steps) => integer(),
      optional(:target_position) => %{x: number(), y: number()},
      optional(:timeout) => number() | :infinity,
      optional(:trial) => boolean()
    }

Options of drag_to/3, see its docs.

drop_options()

@type drop_options() ::
  [
    position: %{x: number(), y: number()},
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:position) => %{x: number(), y: number()},
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of drop/3, see its docs.

element_handle_options()

@type element_handle_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of element_handle/2, see its docs.

evaluate_handle_options()

@type evaluate_handle_options() ::
  [
    expose_functions: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:expose_functions) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of evaluate_handle/4, see its docs.

evaluate_options()

@type evaluate_options() ::
  [
    expose_functions: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:expose_functions) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of evaluate/4, see its docs.

fill_options()

@type fill_options() ::
  [
    force: boolean(),
    no_wait_after: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:force) => boolean(),
      optional(:no_wait_after) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of fill/3, see its docs.

filter_options()

@type filter_options() ::
  [
    has: t(),
    has_not: t(),
    has_not_text: String.t() | Regex.t() | Playwright.RegExp.t(),
    has_text: String.t() | Regex.t() | Playwright.RegExp.t(),
    visible: boolean()
  ]
  | %{
      optional(:has) => t(),
      optional(:has_not) => t(),
      optional(:has_not_text) => String.t() | Regex.t() | Playwright.RegExp.t(),
      optional(:has_text) => String.t() | Regex.t() | Playwright.RegExp.t(),
      optional(:visible) => boolean()
    }

Options of filter/2, see its docs.

focus_options()

@type focus_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of focus/2, see its docs.

get_attribute_options()

@type get_attribute_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of get_attribute/3, see its docs.

get_by_alt_text_options()

@type get_by_alt_text_options() ::
  [{:exact, boolean()}] | %{optional(:exact) => boolean()}

Options of get_by_alt_text/3, see its docs.

get_by_label_options()

@type get_by_label_options() ::
  [{:exact, boolean()}] | %{optional(:exact) => boolean()}

Options of get_by_label/3, see its docs.

get_by_placeholder_options()

@type get_by_placeholder_options() ::
  [{:exact, boolean()}] | %{optional(:exact) => boolean()}

Options of get_by_placeholder/3, see its docs.

get_by_role_options()

@type get_by_role_options() ::
  [
    checked: boolean(),
    description: String.t() | Regex.t() | Playwright.RegExp.t(),
    disabled: boolean(),
    exact: boolean(),
    expanded: boolean(),
    include_hidden: boolean(),
    level: integer(),
    name: String.t() | Regex.t() | Playwright.RegExp.t(),
    pressed: boolean(),
    selected: boolean()
  ]
  | %{
      optional(:checked) => boolean(),
      optional(:description) => String.t() | Regex.t() | Playwright.RegExp.t(),
      optional(:disabled) => boolean(),
      optional(:exact) => boolean(),
      optional(:expanded) => boolean(),
      optional(:include_hidden) => boolean(),
      optional(:level) => integer(),
      optional(:name) => String.t() | Regex.t() | Playwright.RegExp.t(),
      optional(:pressed) => boolean(),
      optional(:selected) => boolean()
    }

Options of get_by_role/3, see its docs.

get_by_text_options()

@type get_by_text_options() ::
  [{:exact, boolean()}] | %{optional(:exact) => boolean()}

Options of get_by_text/3, see its docs.

get_by_title_options()

@type get_by_title_options() ::
  [{:exact, boolean()}] | %{optional(:exact) => boolean()}

Options of get_by_title/3, see its docs.

highlight_options()

@type highlight_options() ::
  [{:style, String.t() | %{optional(String.t()) => String.t() | number()}}]
  | %{
      optional(:style) =>
        String.t() | %{optional(String.t()) => String.t() | number()}
    }

Options of highlight/2, see its docs.

hover_options()

@type hover_options() ::
  [
    force: boolean(),
    modifiers: [:alt | :control | :control_or_meta | :meta | :shift],
    no_wait_after: boolean(),
    position: %{x: number(), y: number()},
    scroll: :auto | :none,
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity,
    trial: boolean()
  ]
  | %{
      optional(:force) => boolean(),
      optional(:modifiers) => [
        :alt | :control | :control_or_meta | :meta | :shift
      ],
      optional(:no_wait_after) => boolean(),
      optional(:position) => %{x: number(), y: number()},
      optional(:scroll) => :auto | :none,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity,
      optional(:trial) => boolean()
    }

Options of hover/2, see its docs.

inner_html_options()

@type inner_html_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of inner_html/2, see its docs.

inner_text_options()

@type inner_text_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of inner_text/2, see its docs.

input_value_options()

@type input_value_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of input_value/2, see its docs.

is_checked_options()

@type is_checked_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of is_checked/2, see its docs.

is_disabled_options()

@type is_disabled_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of is_disabled/2, see its docs.

is_editable_options()

@type is_editable_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of is_editable/2, see its docs.

is_enabled_options()

@type is_enabled_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of is_enabled/2, see its docs.

is_hidden_options()

@type is_hidden_options() ::
  [{:timeout, number() | :infinity}]
  | %{optional(:timeout) => number() | :infinity}

Options of is_hidden/2, see its docs.

is_visible_options()

@type is_visible_options() ::
  [{:timeout, number() | :infinity}]
  | %{optional(:timeout) => number() | :infinity}

Options of is_visible/2, see its docs.

locator_options()

@type locator_options() ::
  [
    has: t(),
    has_not: t(),
    has_not_text: String.t() | Regex.t() | Playwright.RegExp.t(),
    has_text: String.t() | Regex.t() | Playwright.RegExp.t()
  ]
  | %{
      optional(:has) => t(),
      optional(:has_not) => t(),
      optional(:has_not_text) => String.t() | Regex.t() | Playwright.RegExp.t(),
      optional(:has_text) => String.t() | Regex.t() | Playwright.RegExp.t()
    }

Options of locator/3, see its docs.

press_options()

@type press_options() ::
  [
    delay: number(),
    no_wait_after: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:delay) => number(),
      optional(:no_wait_after) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of press/3, see its docs.

press_sequentially_options()

@type press_sequentially_options() ::
  [
    delay: number(),
    no_wait_after: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:delay) => number(),
      optional(:no_wait_after) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of press_sequentially/3, see its docs.

screenshot_options()

@type screenshot_options() ::
  [
    animations: :disabled | :allow,
    caret: :hide | :initial,
    mask: [t()],
    mask_color: String.t(),
    omit_background: boolean(),
    path: String.t(),
    quality: integer(),
    scale: :css | :device,
    signal: Playwright.AbortSignal.t(),
    style: String.t(),
    timeout: number() | :infinity,
    type: :png | :jpeg | :webp
  ]
  | %{
      optional(:animations) => :disabled | :allow,
      optional(:caret) => :hide | :initial,
      optional(:mask) => [t()],
      optional(:mask_color) => String.t(),
      optional(:omit_background) => boolean(),
      optional(:path) => String.t(),
      optional(:quality) => integer(),
      optional(:scale) => :css | :device,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:style) => String.t(),
      optional(:timeout) => number() | :infinity,
      optional(:type) => :png | :jpeg | :webp
    }

Options of screenshot/2, see its docs.

scroll_into_view_if_needed_options()

@type scroll_into_view_if_needed_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of scroll_into_view_if_needed/2, see its docs.

select_option_options()

@type select_option_options() ::
  [
    force: boolean(),
    no_wait_after: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:force) => boolean(),
      optional(:no_wait_after) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of select_option/3, see its docs.

select_text_options()

@type select_text_options() ::
  [
    force: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:force) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of select_text/2, see its docs.

set_checked_options()

@type set_checked_options() ::
  [
    force: boolean(),
    no_wait_after: boolean(),
    position: %{x: number(), y: number()},
    scroll: :auto | :none,
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity,
    trial: boolean()
  ]
  | %{
      optional(:force) => boolean(),
      optional(:no_wait_after) => boolean(),
      optional(:position) => %{x: number(), y: number()},
      optional(:scroll) => :auto | :none,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity,
      optional(:trial) => boolean()
    }

Options of set_checked/3, see its docs.

set_input_files_options()

@type set_input_files_options() ::
  [
    no_wait_after: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:no_wait_after) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of set_input_files/3, see its docs.

t()

@type t() :: %Playwright.Locator{_apiName: term(), _frame: term(), _selector: term()}

A Locator handle. Compare handles with ==, their fields are internal.

tap_options()

@type tap_options() ::
  [
    force: boolean(),
    modifiers: [:alt | :control | :control_or_meta | :meta | :shift],
    no_wait_after: boolean(),
    position: %{x: number(), y: number()},
    scroll: :auto | :none,
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity,
    trial: boolean()
  ]
  | %{
      optional(:force) => boolean(),
      optional(:modifiers) => [
        :alt | :control | :control_or_meta | :meta | :shift
      ],
      optional(:no_wait_after) => boolean(),
      optional(:position) => %{x: number(), y: number()},
      optional(:scroll) => :auto | :none,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity,
      optional(:trial) => boolean()
    }

Options of tap/2, see its docs.

text_content_options()

@type text_content_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of text_content/2, see its docs.

type_options()

@type type_options() ::
  [
    delay: number(),
    no_wait_after: boolean(),
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity
  ]
  | %{
      optional(:delay) => number(),
      optional(:no_wait_after) => boolean(),
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of type/3, see its docs.

uncheck_options()

@type uncheck_options() ::
  [
    force: boolean(),
    no_wait_after: boolean(),
    position: %{x: number(), y: number()},
    scroll: :auto | :none,
    signal: Playwright.AbortSignal.t(),
    timeout: number() | :infinity,
    trial: boolean()
  ]
  | %{
      optional(:force) => boolean(),
      optional(:no_wait_after) => boolean(),
      optional(:position) => %{x: number(), y: number()},
      optional(:scroll) => :auto | :none,
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity,
      optional(:trial) => boolean()
    }

Options of uncheck/2, see its docs.

wait_for_function_options()

@type wait_for_function_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of wait_for_function/4, see its docs.

wait_for_options()

@type wait_for_options() ::
  [
    signal: Playwright.AbortSignal.t(),
    state: :attached | :detached | :visible | :hidden,
    timeout: number() | :infinity
  ]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:state) => :attached | :detached | :visible | :hidden,
      optional(:timeout) => number() | :infinity
    }

Options of wait_for/2, see its docs.

Functions

all(locator)

@spec all(t()) :: {:ok, [t()]} | {:error, Playwright.error()}

When the locator points to a list of elements, this returns an array of locators, pointing to their respective elements.

Note

Playwright.Locator.all/1 does not wait for elements to match the locator, and instead immediately returns whatever is present in the page.

When the list of elements changes dynamically, Playwright.Locator.all/1 will produce unpredictable and flaky results.

When the list of elements is stable, but loaded dynamically, wait for the full list to finish loading before calling Playwright.Locator.all/1.

Usage

for li <- Page.get_by_role(page, :listitem) |> Locator.all!() do
  Locator.click!(li)
end

Since Playwright 1.29.

all!(locator)

@spec all!(t()) :: [t()]

Like all/1, but returns the value and raises on error.

all_inner_texts(locator)

@spec all_inner_texts(t()) :: {:ok, [String.t()]} | {:error, Playwright.error()}

Returns an array of node.innerText values for all matching nodes.

Asserting text

If you need to assert text on the page, prefer Playwright.Expect.to_have_text/3 with :use_inner_text option to avoid flakiness. See assertions guide for more details.

Usage

texts = Page.get_by_role(page, :link) |> Locator.all_inner_texts!()

Since Playwright 1.14.

all_inner_texts!(locator)

@spec all_inner_texts!(t()) :: [String.t()]

Like all_inner_texts/1, but returns the value and raises on error.

all_text_contents(locator)

@spec all_text_contents(t()) :: {:ok, [String.t()]} | {:error, Playwright.error()}

Returns an array of node.textContent values for all matching nodes.

Asserting text

If you need to assert text on the page, prefer Playwright.Expect.to_have_text/3 to avoid flakiness. See assertions guide for more details.

Usage

texts = Page.get_by_role(page, :link) |> Locator.all_text_contents!()

Since Playwright 1.14.

all_text_contents!(locator)

@spec all_text_contents!(t()) :: [String.t()]

Like all_text_contents/1, but returns the value and raises on error.

and_(locator, other_locator)

@spec and_(t(), t()) :: t()

Creates a locator that matches both this locator and the argument locator.

Usage

The following example finds a button with a specific title.

button = Page.get_by_role(page, :button) |> Locator.and_(Page.get_by_title(page, "Subscribe"))

Since Playwright 1.34.

Arguments

  • other_locator - Additional locator to match.

aria_snapshot(locator, options \\ [])

@spec aria_snapshot(t(), aria_snapshot_options()) ::
  {:ok, String.t()} | {:error, Playwright.error()}

Captures the aria snapshot of the given element. Read more about aria snapshots and Playwright.Expect.to_match_aria_snapshot/3 for the corresponding assertion.

Usage

Page.get_by_role(page, :link) |> Locator.aria_snapshot!()

Details

This method captures the aria snapshot of the given element. The snapshot is a string that represents the state of the element and its children. The snapshot can be used to assert the state of the element in the test, or to compare it to state in the future.

The ARIA snapshot is represented using YAML markup language:

  • The keys of the objects are the roles and optional accessible names of the elements.
  • The values are either text content or an array of child elements.
  • Generic static text can be represented with the text key.

Below is the HTML markup and the respective ARIA snapshot:

<ul aria-label="Links">
  <li><a href="/">Home</a></li>
  <li><a href="/about">About</a></li>
<ul>
- list "Links":
  - listitem:
    - link "Home"
  - listitem:
    - link "About"

An AI-optimized snapshot, controlled by :mode, is different from a default snapshot:

  1. Includes element references [ref=e2].
    1. Does not wait for an element matching the locator, and fails when no elements match.
    2. Includes snapshots of <iframe>s inside the target.
    3. Can omit names that repeat printed descendants, such as a heading containing a link. Names that identify dialogs, landmarks, groups, tables and table headers are retained.

Since Playwright 1.49.

Options

aria_snapshot!(locator, options \\ [])

@spec aria_snapshot!(t(), aria_snapshot_options()) :: String.t()

Like aria_snapshot/2, but returns the value and raises on error.

aria_snapshot_json(locator, options \\ [])

@spec aria_snapshot_json(t(), aria_snapshot_json_options()) ::
  {:ok, term()} | {:error, Playwright.error()}

Captures the aria snapshot of the given element as a free form JSON object.

Usage

Page.get_by_role(page, :list) |> Locator.aria_snapshot_json!()

Details

This method returns the same tree as Playwright.Locator.aria_snapshot/2, serialized as a JSON value instead of YAML markup. The result is a list of nodes, each node being an object with the following properties:

  • role Aria role of the element, or "text" for a static text fragment.
  • name Accessible name of the element, if any.
  • text Text content of the element when it is the only child, or the content of a static text fragment.
  • children Child nodes and text fragments.
  • Boolean and value properties for element state flags: checked, disabled, expanded, active, invalid, level, pressed and selected.
  • Additional element properties, for example url for links and placeholder for text boxes.
  • ref Element reference for AI-optimized snapshots.
  • cursor Set to "pointer" for clickable elements in AI-optimized snapshots.
  • box Bounding box of the element when :boxes is set.

Since Playwright 1.63.

Options

aria_snapshot_json!(locator, options \\ [])

@spec aria_snapshot_json!(t(), aria_snapshot_json_options()) :: term()

Like aria_snapshot_json/2, but returns the value and raises on error.

blur(locator, options \\ [])

@spec blur(t(), blur_options()) :: :ok | {:error, Playwright.error()}

Calls blur on the element.

Since Playwright 1.28.

Options

blur!(locator, options \\ [])

@spec blur!(t(), blur_options()) :: :ok

Like blur/2, but returns the value and raises on error.

bounding_box(locator, options \\ [])

@spec bounding_box(t(), bounding_box_options()) ::
  {:ok, nil | %{x: number(), y: number(), width: number(), height: number()}}
  | {:error, Playwright.error()}

This method returns the bounding box of the element matching the locator, or nil 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.

Details

Scrolling affects the returned bounding 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.

Usage

box = Page.get_by_role(page, :button) |> Locator.bounding_box!()
Mouse.click!(Page.mouse(page), box.x + box.width / 2, box.y + box.height / 2)

Since Playwright 1.14.

Options

bounding_box!(locator, options \\ [])

@spec bounding_box!(t(), bounding_box_options()) ::
  nil | %{x: number(), y: number(), width: number(), height: number()}

Like bounding_box/2, but returns the value and raises on error.

check(locator, options \\ [])

@spec check(t(), check_options()) :: :ok | {:error, Playwright.error()}

Ensure that checkbox or radio element is checked.

Details

Performs the following steps:

  1. Ensure that element is a checkbox or a radio input. If not, this method fails. 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/1 to click in the center of the element.
  5. Ensure that the element is now checked. If not, this method fails.

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

When all steps combined have not finished during the specified :timeout, this method fails with a TimeoutError. Passing zero timeout disables this.

Usage

Page.get_by_role(page, :checkbox) |> Locator.check!()

Since Playwright 1.14.

Options

  • :force (boolean/0) - Whether to bypass the actionability checks. Defaults to false.

  • :no_wait_after (boolean/0) - This option has no effect.

  • :position (map/0) - A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.

    • :x
    • :y
  • :scroll (:auto | :none) - Since Playwright 1.62. Controls whether Playwright scrolls the element into view before performing the action. Defaults to :auto, which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to :none, Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :trial (boolean/0) - When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it.

check!(locator, options \\ [])

@spec check!(t(), check_options()) :: :ok

Like check/2, but returns the value and raises on error.

clear(locator, options \\ [])

@spec clear(t(), clear_options()) :: :ok | {:error, Playwright.error()}

Clear the input field.

Details

This method waits for actionability checks, focuses the element, clears it and triggers an input event after clearing.

If the target element is not an <input>, <textarea> or [contenteditable] element, this method fails. However, if the element is inside the <label> element that has an associated control, the control will be cleared instead.

Usage

Page.get_by_role(page, :textbox) |> Locator.clear!()

Since Playwright 1.28.

Options

clear!(locator, options \\ [])

@spec clear!(t(), clear_options()) :: :ok

Like clear/2, but returns the value and raises on error.

click(locator, options \\ [])

@spec click(t(), click_options()) :: :ok | {:error, Playwright.error()}

Click an element.

Details

This method clicks the element by performing the following steps:

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

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

When all steps combined have not finished during the specified :timeout, this method fails with a TimeoutError. Passing zero timeout disables this.

Usage

Click a button:

Page.get_by_role(page, :button) |> Locator.click!()

Shift-right-click at a specific position on a canvas:

Page.locator(page, "canvas")
|> Locator.click!(button: :right, modifiers: [:shift], position: %{x: 23, y: 32})

Since Playwright 1.14.

Options

  • :button (:left | :right | :middle) - Defaults to :left.

  • :click_count (integer/0) - defaults to 1. See UIEvent.detail.

  • :delay (number/0) - Time to wait between mousedown and mouseup in milliseconds. Defaults to 0.

  • :force (boolean/0) - Whether to bypass the actionability checks. Defaults to false.

  • :modifiers (list of :alt | :control | :control_or_meta | :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. "ControlOrMeta" resolves to "Control" on Windows and Linux and to "Meta" on macOS.

  • :no_wait_after (boolean/0) - 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. Defaults to false.

  • :position (map/0) - A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.

    • :x
    • :y
  • :scroll (:auto | :none) - Since Playwright 1.62. Controls whether Playwright scrolls the element into view before performing the action. Defaults to :auto, which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to :none, Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :steps (integer/0) - Since Playwright 1.57. Defaults to 1. Sends n interpolated mousemove events to represent travel between Playwright's current cursor position and the provided destination. When set to 1, emits a single mousemove event at the destination location.

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :trial (boolean/0) - When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it. Note that keyboard :modifiers will be pressed regardless of :trial to allow testing elements which are only visible when those keys are pressed.

click!(locator, options \\ [])

@spec click!(t(), click_options()) :: :ok

Like click/2, but returns the value and raises on error.

content_frame(locator)

@spec content_frame(t()) :: Playwright.FrameLocator.t()

Returns a FrameLocator object pointing to the same iframe as this locator.

Useful when you have a Locator object obtained somewhere, and later on would like to interact with the content inside the frame.

For a reverse operation, use Playwright.FrameLocator.owner/1.

Usage

locator = Page.locator(page, ~s(iframe[name="embedded"]))
# ...
frame_locator = Locator.content_frame(locator)
FrameLocator.get_by_role(frame_locator, :button) |> Locator.click!()

Since Playwright 1.43.

count(locator)

@spec count(t()) :: {:ok, integer()} | {:error, Playwright.error()}

Returns the number of elements matching the locator.

Asserting count

If you need to assert the number of elements on the page, prefer Playwright.Expect.to_have_count/3 to avoid flakiness. See assertions guide for more details.

Usage

count = Page.get_by_role(page, :listitem) |> Locator.count!()

Since Playwright 1.14.

count!(locator)

@spec count!(t()) :: integer()

Like count/1, but returns the value and raises on error.

dblclick(locator, options \\ [])

@spec dblclick(t(), dblclick_options()) :: :ok | {:error, Playwright.error()}

Double-click an element.

Details

This method double clicks the element by performing the following steps:

  1. Wait for actionability checks on the element, unless :force option is set.
  2. Scroll the element into view if needed.
  3. Use Playwright.Page.mouse/1 to double click in the center of the element, or the specified :position.

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

When all steps combined have not finished during the specified :timeout, this method fails with a TimeoutError. Passing zero timeout disables this.

Note

Playwright.Locator.dblclick/2 dispatches two click events and a single dblclick event.

Since Playwright 1.14.

Options

  • :button (:left | :right | :middle) - Defaults to :left.

  • :delay (number/0) - Time to wait between mousedown and mouseup in milliseconds. Defaults to 0.

  • :force (boolean/0) - Whether to bypass the actionability checks. Defaults to false.

  • :modifiers (list of :alt | :control | :control_or_meta | :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. "ControlOrMeta" resolves to "Control" on Windows and Linux and to "Meta" on macOS.

  • :no_wait_after (boolean/0) - This option has no effect.

  • :position (map/0) - A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.

    • :x
    • :y
  • :scroll (:auto | :none) - Since Playwright 1.62. Controls whether Playwright scrolls the element into view before performing the action. Defaults to :auto, which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to :none, Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :steps (integer/0) - Since Playwright 1.57. Defaults to 1. Sends n interpolated mousemove events to represent travel between Playwright's current cursor position and the provided destination. When set to 1, emits a single mousemove event at the destination location.

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :trial (boolean/0) - When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it. Note that keyboard :modifiers will be pressed regardless of :trial to allow testing elements which are only visible when those keys are pressed.

dblclick!(locator, options \\ [])

@spec dblclick!(t(), dblclick_options()) :: :ok

Like dblclick/2, but returns the value and raises on error.

describe(locator, description)

@spec describe(t(), String.t()) :: t()

Describes the locator, description is used in the trace viewer and reports. Returns the locator pointing to the same element.

Usage

button = Page.get_by_test_id(page, "btn-sub") |> Locator.describe("Subscribe button")
Locator.click!(button)

Since Playwright 1.53.

Arguments

  • description - Locator description.

description(locator)

@spec description(t()) :: nil | String.t()

Returns locator description previously set with Playwright.Locator.describe/2. Returns nil if no custom description has been set. Prefer Playwright.Locator.to_string/1 for a human-readable representation, as it uses the description when available.

Usage

button = Page.get_by_role(page, :button) |> Locator.describe("Subscribe button")
Locator.description(button)
# => "Subscribe button"

input = Page.get_by_role(page, :textbox)
Locator.description(input)
# => nil

Since Playwright 1.57.

dispatch_event(locator, type, event_init \\ nil, options \\ [])

@spec dispatch_event(t(), String.t(), term() | nil, dispatch_event_options()) ::
  :ok | {:error, Playwright.error()}

Programmatically dispatch an event on the matching element.

Usage

Locator.dispatch_event!(locator, "click")

Details

The snippet above dispatches the click event on the element. Regardless of the visibility state of the element, click is dispatched. This is equivalent to calling element.click().

Under the hood, it creates an instance of an event based on the given type, initializes it with event_init properties and dispatches it on the element. Events are composed, cancelable and bubble by default.

Since event_init is event-specific, please refer to the events documentation for the lists of initial properties:

You can also specify JSHandle as the property value if you want live objects to be passed into the event:

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

Since Playwright 1.14.

Arguments

  • type - DOM event type: "click", "dragstart", etc.
  • event_init - Optional event-specific initialization properties.

Options

dispatch_event!(locator, type, event_init \\ nil, options \\ [])

@spec dispatch_event!(t(), String.t(), term() | nil, dispatch_event_options()) :: :ok

Like dispatch_event/4, but returns the value and raises on error.

drag_to(locator, target, options \\ [])

@spec drag_to(t(), t(), drag_to_options()) :: :ok | {:error, Playwright.error()}

Drag the source element towards the target element and drop it.

Details

This method drags the locator to another target locator or target position. It will first move to the source element, perform a mousedown, then move to the target element or position and perform a mouseup.

Usage

source = Page.locator(page, "#source")
target = Page.locator(page, "#target")

Locator.drag_to!(source, target)
# or specify exact positions relative to the top-left corners of the elements:
Locator.drag_to!(source, target, source_position: %{x: 34, y: 7}, target_position: %{x: 10, y: 20})

Since Playwright 1.18.

Arguments

  • target - Locator of the element to drag to.

Options

  • :force (boolean/0) - Whether to bypass the actionability checks. Defaults to false.

  • :no_wait_after (boolean/0) - This option has no effect.

  • :scroll (:auto | :none) - Since Playwright 1.62. Controls whether Playwright scrolls the element into view before performing the action. Defaults to :auto, which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to :none, Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :source_position (map/0) - Clicks on the source element at this point relative to the top-left corner of the element's padding box. If not specified, some visible point of the element is used.

    • :x
    • :y
  • :steps (integer/0) - Since Playwright 1.57. Defaults to 1. Sends n interpolated mousemove events to represent travel between the mousedown and mouseup of the drag. When set to 1, emits a single mousemove event at the destination location.

  • :target_position (map/0) - Drops on the target element at this point relative to the top-left corner of the element's padding box. If not specified, some visible point of the element is used.

    • :x
    • :y
  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :trial (boolean/0) - When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it.

drag_to!(locator, target, options \\ [])

@spec drag_to!(t(), t(), drag_to_options()) :: :ok

Like drag_to/3, but returns the value and raises on error.

drop(locator, payload, options \\ [])

@spec drop(
  t(),
  %{
    optional(:files) =>
      String.t()
      | [String.t()]
      | %{name: String.t(), mimeType: String.t(), buffer: binary()}
      | [%{name: String.t(), mimeType: String.t(), buffer: binary()}],
    optional(:data) => %{optional(String.t()) => String.t()}
  },
  drop_options()
) :: :ok | {:error, Playwright.error()}

Simulate an external drag-and-drop of files or clipboard-like data onto this locator.

Details

Dispatches the native dragenter, dragover, and drop events at the center of the target element with a synthetic DataTransfer carrying the provided files and/or data entries. Works cross-browser by constructing the DataTransfer in the page context.

If the target element's dragover listener does not call preventDefault(), the target is considered to have rejected the drop: Playwright dispatches dragleave and this method fails.

Usage

Drop a file buffer onto an upload area:

Page.locator(page, "#dropzone")
|> Locator.drop!(%{files: %{name: "note.txt", mimeType: "text/plain", buffer: "hello"}})

Drop plain text and a URL together:

Page.locator(page, "#dropzone")
|> Locator.drop!(%{data: %{"text/plain" => "hello world", "text/uri-list" => "https://example.com"}})

Since Playwright 1.60.

Arguments

  • payload - Data to drop onto the target. Provide files (file paths or in-memory buffers), data (a mime-type → string map for clipboard-like content such as text/plain, text/html, text/uri-list), or both.
    • :files
      • :name - File name
      • :mimeType - File type
      • :buffer - File content
    • :data

Options

drop!(locator, payload, options \\ [])

@spec drop!(
  t(),
  %{
    optional(:files) =>
      String.t()
      | [String.t()]
      | %{name: String.t(), mimeType: String.t(), buffer: binary()}
      | [%{name: String.t(), mimeType: String.t(), buffer: binary()}],
    optional(:data) => %{optional(String.t()) => String.t()}
  },
  drop_options()
) :: :ok

Like drop/3, but returns the value and raises on error.

element_handle(locator, options \\ [])

@spec element_handle(t(), element_handle_options()) ::
  {:ok, Playwright.ElementHandle.t()} | {:error, Playwright.error()}

Resolves given locator to the first matching DOM element. If there are no matching elements, waits for one. If multiple elements match the locator, fails.

Discouraged

Always prefer using Locators and web assertions over ElementHandles because latter are inherently racy.

Since Playwright 1.14.

Options

element_handle!(locator, options \\ [])

@spec element_handle!(t(), element_handle_options()) :: Playwright.ElementHandle.t()

Like element_handle/2, but returns the value and raises on error.

element_handles(locator)

@spec element_handles(t()) ::
  {:ok, [Playwright.ElementHandle.t()]} | {:error, Playwright.error()}

Resolves given locator to all matching DOM elements. If there are no matching elements, returns an empty list.

Discouraged

Always prefer using Locators and web assertions over ElementHandles because latter are inherently racy.

Since Playwright 1.14.

element_handles!(locator)

@spec element_handles!(t()) :: [Playwright.ElementHandle.t()]

Like element_handles/1, but returns the value and raises on error.

evaluate(locator, expression, arg \\ nil, options \\ [])

@spec evaluate(t(), String.t(), term() | nil, evaluate_options()) ::
  {:ok, term()} | {:error, Playwright.error()}

Execute JavaScript code in the page, taking the matching element as an argument.

Details

Returns the return value of expression, called with the matching element as a first argument, and arg as a second argument.

If expression returns a Promise, this method will wait for the promise to resolve and return its value.

If expression throws or rejects, this method fails.

Usage

Passing argument to expression:

result =
  Page.get_by_test_id(page, "myId")
  |> Locator.evaluate!("(element, [x, y]) => element.textContent + ' ' + x * y", [7, 8])

# prints "myId text 56"
IO.puts(result)

Since Playwright 1.14.

Arguments

  • expression - Function to be evaluated in the page context.
  • arg - Optional argument to pass to expression.

Options

  • :expose_functions (boolean/0) - Since Playwright 1.62. When set to true, functions passed inside arg are exposed in the page and can be called from the page function. Calling one returns a Promise of its result. Under the hood, each function is exposed via Playwright.Page.expose_function/3, so it is technically accessible from all frames of the page. Exposed functions are cleared upon the top-level navigation. Defaults to false, in which case functions are not serializable and passing one fails.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds to wait for the locator before evaluating. Note that after locator is resolved, evaluation itself is not limited by the timeout. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

evaluate!(locator, expression, arg \\ nil, options \\ [])

@spec evaluate!(t(), String.t(), term() | nil, evaluate_options()) :: term()

Like evaluate/4, but returns the value and raises on error.

evaluate_all(locator, expression, arg \\ nil)

@spec evaluate_all(t(), String.t(), term() | nil) ::
  {:ok, term()} | {:error, Playwright.error()}

Execute JavaScript code in the page, taking all matching elements as an argument.

Details

Returns the return value of expression, called with an array of all matching elements as a first argument, and arg as a second argument.

If expression returns a Promise, this method will wait for the promise to resolve and return its value.

If expression throws or rejects, this method fails.

Usage

locator = Page.locator(page, "div")
more_than_ten = Locator.evaluate_all!(locator, "(divs, min) => divs.length > min", 10)

Since Playwright 1.14.

Arguments

  • expression - Function to be evaluated in the page context.
  • arg - Optional argument to pass to expression.

evaluate_all!(locator, expression, arg \\ nil)

@spec evaluate_all!(t(), String.t(), term() | nil) :: term()

Like evaluate_all/3, but returns the value and raises on error.

evaluate_handle(locator, expression, arg \\ nil, options \\ [])

@spec evaluate_handle(t(), String.t(), term() | nil, evaluate_handle_options()) ::
  {:ok, Playwright.JSHandle.t()} | {:error, Playwright.error()}

Execute JavaScript code in the page, taking the matching element as an argument, and return a JSHandle with the result.

Details

Returns the return value of expression as aJSHandle, called with the matching element as a first argument, and arg as a second argument.

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

If expression returns a Promise, this method will wait for the promise to resolve and return its value.

If expression throws or rejects, this method fails.

See Playwright.Page.evaluate_handle/4 for more details.

Since Playwright 1.14.

Arguments

  • expression - Function to be evaluated in the page context.
  • arg - Optional argument to pass to expression.

Options

  • :expose_functions (boolean/0) - Since Playwright 1.62. When set to true, functions passed inside arg are exposed in the page and can be called from the page function. Calling one returns a Promise of its result. Under the hood, each function is exposed via Playwright.Page.expose_function/3, so it is technically accessible from all frames of the page. Exposed functions are cleared upon the top-level navigation. Defaults to false, in which case functions are not serializable and passing one fails.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds to wait for the locator before evaluating. Note that after locator is resolved, evaluation itself is not limited by the timeout. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

evaluate_handle!(locator, expression, arg \\ nil, options \\ [])

@spec evaluate_handle!(t(), String.t(), term() | nil, evaluate_handle_options()) ::
  Playwright.JSHandle.t()

Like evaluate_handle/4, but returns the value and raises on error.

fill(locator, value, options \\ [])

@spec fill(t(), String.t(), fill_options()) :: :ok | {:error, Playwright.error()}

Set a value to the input field.

Usage

Page.get_by_role(page, :textbox) |> Locator.fill!("example value")

Details

This method waits for actionability checks, focuses the element, fills it and triggers an input event after filling. Note that you can pass an empty string to clear the input field.

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

To send fine-grained keyboard events, use Playwright.Locator.press_sequentially/3.

Since Playwright 1.14.

Arguments

  • value - Value to set for the <input>, <textarea> or [contenteditable] element.

Options

fill!(locator, value, options \\ [])

@spec fill!(t(), String.t(), fill_options()) :: :ok

Like fill/3, but returns the value and raises on error.

filter(locator, options \\ [])

@spec filter(t(), filter_options()) :: t()

This method narrows existing locator according to the options, for example filters by text. It can be chained to filter multiple times.

Usage

row_locator = Page.locator(page, "tr")
# ...
row_locator
|> Locator.filter(has_text: "text in column 1")
|> Locator.filter(has: Page.get_by_role(page, :button, name: "column 2 button"))
|> Locator.screenshot!()

Since Playwright 1.22.

Options

  • :has (Playwright.Locator.t/0) - Narrows down the results of the method to those which contain elements matching this relative locator. For example, article that has text=Playwright matches <article><div>Playwright</div></article>.

    Inner locator must be relative to the outer locator and is queried starting with the outer locator match, not the document root. For example, you can find content that has div in <article><content><div>Playwright</div></content></article>. However, looking for content that has article div will fail, because the inner locator must be relative and should not use any elements outside the content.

    Note that outer and inner locators must belong to the same frame. Inner locator must not contain FrameLocators.

  • :has_not (Playwright.Locator.t/0) - Since Playwright 1.33. Matches elements that do not contain an element that matches an inner locator. Inner locator is queried against the outer one. For example, article that does not have div matches <article><span>Playwright</span></article>.

    Note that outer and inner locators must belong to the same frame. Inner locator must not contain FrameLocators.

  • :has_not_text (String.t/0 | Regex.t/0 | Playwright.RegExp.t/0) - Since Playwright 1.33. Matches elements that do not contain specified text somewhere inside, possibly in a child or a descendant element. When passed a string, matching is case-insensitive and searches for a substring.

  • :has_text (String.t/0 | Regex.t/0 | Playwright.RegExp.t/0) - Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. When passed a string, matching is case-insensitive and searches for a substring. For example, "Playwright" matches <article><div>Playwright</div></article>.

  • :visible (boolean/0) - Since Playwright 1.51. Only matches visible or invisible elements. Prefer the Playwright.Locator.visible/1 shortcut when matching only visible elements.

first(locator)

@spec first(t()) :: t()

Returns locator to the first matching element.

Since Playwright 1.14.

focus(locator, options \\ [])

@spec focus(t(), focus_options()) :: :ok | {:error, Playwright.error()}

Calls focus on the matching element.

Since Playwright 1.14.

Options

focus!(locator, options \\ [])

@spec focus!(t(), focus_options()) :: :ok

Like focus/2, but returns the value and raises on error.

frame_locator(locator, selector)

@spec frame_locator(t(), String.t()) :: Playwright.FrameLocator.t()

When working with iframes, you can create a frame locator that will enter the iframe and allow locating elements in that iframe:

Usage

locator = Page.frame_locator(page, "iframe") |> FrameLocator.get_by_text("Submit")
Locator.click!(locator)

Since Playwright 1.17.

Arguments

  • selector - A selector to use when resolving DOM element.

get_attribute(locator, name, options \\ [])

@spec get_attribute(t(), String.t(), get_attribute_options()) ::
  {:ok, nil | String.t()} | {:error, Playwright.error()}

Returns the matching element's attribute value.

Asserting attributes

If you need to assert an element's attribute, prefer Playwright.Expect.to_have_attribute/4 to avoid flakiness. See assertions guide for more details.

Since Playwright 1.14.

Arguments

  • name - Attribute name to get the value for.

Options

get_attribute!(locator, name, options \\ [])

@spec get_attribute!(t(), String.t(), get_attribute_options()) :: nil | String.t()

Like get_attribute/3, but returns the value and raises on error.

get_by_alt_text(locator, text, options \\ [])

@spec get_by_alt_text(
  t(),
  String.t() | Regex.t() | Playwright.RegExp.t(),
  get_by_alt_text_options()
) ::
  t()

Allows locating elements by their alt text.

Usage

For example, this method will find the image by alt text "Playwright logo":

<img alt='Playwright logo'>
Page.get_by_alt_text(page, "Playwright logo") |> Locator.click!()

Since Playwright 1.27.

Arguments

  • text - Text to locate the element for.

Options

  • :exact (boolean/0) - Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

get_by_label(locator, text, options \\ [])

@spec get_by_label(
  t(),
  String.t() | Regex.t() | Playwright.RegExp.t(),
  get_by_label_options()
) :: t()

Allows locating input elements by the text of the associated <label> or aria-labelledby element, or by the aria-label attribute.

Usage

For example, this method will find inputs by label "Username" and "Password" in the following DOM:

<input aria-label="Username">
<label for="password-input">Password:</label>
<input id="password-input">
Page.get_by_label(page, "Username") |> Locator.fill!("john")
Page.get_by_label(page, "Password") |> Locator.fill!("secret")

Since Playwright 1.27.

Arguments

  • text - Text to locate the element for.

Options

  • :exact (boolean/0) - Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

get_by_placeholder(locator, text, options \\ [])

@spec get_by_placeholder(
  t(),
  String.t() | Regex.t() | Playwright.RegExp.t(),
  get_by_placeholder_options()
) :: t()

Allows locating input elements by the placeholder text.

Usage

For example, consider the following DOM structure.

<input type="email" placeholder="name@example.com" />

You can fill the input after locating it by the placeholder text:

Page.get_by_placeholder(page, "name@example.com") |> Locator.fill!("playwright@microsoft.com")

Since Playwright 1.27.

Arguments

  • text - Text to locate the element for.

Options

  • :exact (boolean/0) - Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

get_by_role(locator, role, options \\ [])

@spec get_by_role(
  t(),
  :alert
  | :alertdialog
  | :application
  | :article
  | :banner
  | :blockquote
  | :button
  | :caption
  | :cell
  | :checkbox
  | :code
  | :columnheader
  | :combobox
  | :complementary
  | :contentinfo
  | :definition
  | :deletion
  | :dialog
  | :directory
  | :document
  | :emphasis
  | :feed
  | :figure
  | :form
  | :generic
  | :grid
  | :gridcell
  | :group
  | :heading
  | :img
  | :insertion
  | :link
  | :list
  | :listbox
  | :listitem
  | :log
  | :main
  | :marquee
  | :math
  | :meter
  | :menu
  | :menubar
  | :menuitem
  | :menuitemcheckbox
  | :menuitemradio
  | :navigation
  | :none
  | :note
  | :option
  | :paragraph
  | :presentation
  | :progressbar
  | :radio
  | :radiogroup
  | :region
  | :row
  | :rowgroup
  | :rowheader
  | :scrollbar
  | :search
  | :searchbox
  | :separator
  | :slider
  | :spinbutton
  | :status
  | :strong
  | :subscript
  | :superscript
  | :switch
  | :tab
  | :table
  | :tablist
  | :tabpanel
  | :term
  | :textbox
  | :time
  | :timer
  | :toolbar
  | :tooltip
  | :tree
  | :treegrid
  | :treeitem
  | String.t(),
  get_by_role_options()
) :: t()

Allows locating elements by their ARIA role, ARIA attributes and accessible name.

Usage

Consider the following DOM structure.

<h3>Sign up</h3>
<label>
  <input type="checkbox" /> Subscribe
</label>
<br/>
<button>Submit</button>

You can locate each element by its implicit role:

Page.get_by_role(page, :heading, name: "Sign up") |> Expect.to_be_visible!()

Page.get_by_role(page, :checkbox, name: "Subscribe") |> Locator.check!()

Page.get_by_role(page, :button, name: ~r/submit/i) |> Locator.click!()

Details

Role selector does not replace accessibility audits and conformance tests, but rather gives early feedback about the ARIA guidelines.

Many html elements have an implicitly defined role that is recognized by the role selector. You can find all the supported roles here. ARIA guidelines do not recommend duplicating implicit roles and attributes by setting role and/or aria-* attributes to default values.

Since Playwright 1.27.

Arguments

  • role - Required aria role.

    In Elixir, a role is an atom of the typespec (:button) or a string, also one the typespec lacks ("mark"): the JS client takes any string.

Options

  • :checked (boolean/0) - An attribute that is usually set by aria-checked or native <input type=checkbox> controls.

    Learn more about aria-checked.

  • :description (String.t/0 | Regex.t/0 | Playwright.RegExp.t/0) - Since Playwright 1.60. Option to match the accessible description. By default, matching is case-insensitive and searches for a substring, use :exact to control this behavior.

    Learn more about accessible description.

  • :disabled (boolean/0) - An attribute that is usually set by aria-disabled or disabled.

    Note

    Unlike most other attributes, disabled is inherited through the DOM hierarchy. Learn more about aria-disabled.

  • :exact (boolean/0) - Since Playwright 1.28. Whether :name and :description are matched exactly: case-sensitive and whole-string. Defaults to false. Ignored when the value is a regular expression. Note that exact match still trims whitespace.

  • :expanded (boolean/0) - An attribute that is usually set by aria-expanded.

    Learn more about aria-expanded.

  • :include_hidden (boolean/0) - Option that controls whether hidden elements are matched. By default, only non-hidden elements, as defined by ARIA, are matched by role selector.

    Learn more about aria-hidden.

  • :level (integer/0) - A number attribute that is usually present for roles heading, listitem, row, treeitem, with default values for <h1>-<h6> elements.

    Learn more about aria-level.

  • :name (String.t/0 | Regex.t/0 | Playwright.RegExp.t/0) - Option to match the accessible name. By default, matching is case-insensitive and searches for a substring, use :exact to control this behavior.

    Learn more about accessible name.

  • :pressed (boolean/0) - An attribute that is usually set by aria-pressed.

    Learn more about aria-pressed.

  • :selected (boolean/0) - An attribute that is usually set by aria-selected.

    Learn more about aria-selected.

get_by_test_id(locator, test_id)

@spec get_by_test_id(t(), String.t() | Regex.t() | Playwright.RegExp.t()) :: t()

Locate element by the test id.

Usage

Consider the following DOM structure.

<button data-testid="directions">Itinéraire</button>

You can locate the element by its test id:

Page.get_by_test_id(page, "directions") |> Locator.click!()

Details

By default, the data-testid attribute is used as a test id. Use Playwright.Selectors.set_test_id_attribute/2 to configure a different test id attribute if necessary.

# Set a custom test id attribute:
Playwright.selectors(playwright) |> Selectors.set_test_id_attribute("data-pw")

Since Playwright 1.27.

Arguments

  • test_id - Id to locate the element by.

get_by_text(locator, text, options \\ [])

@spec get_by_text(
  t(),
  String.t() | Regex.t() | Playwright.RegExp.t(),
  get_by_text_options()
) :: t()

Allows locating elements that contain given text.

See also Playwright.Locator.filter/2 that allows to match by another criteria, like an accessible role, and then filter by the text content.

Usage

Consider the following DOM structure:

<div>Hello <span>world</span></div>
<div>Hello</div>

You can locate by text substring, exact string, or a regular expression:

# Matches <span>
Page.get_by_text(page, "world")

# Matches first <div>
Page.get_by_text(page, "Hello world")

# Matches second <div>
Page.get_by_text(page, "Hello", exact: true)

# Matches both <div>s
Page.get_by_text(page, ~r/Hello/)

# Matches second <div>
Page.get_by_text(page, ~r/^hello$/i)

Details

Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.

Input elements of the type button and submit are matched by their value instead of the text content. For example, locating by text "Log in" matches <input type=button value="Log in">.

Since Playwright 1.27.

Arguments

  • text - Text to locate the element for.

Options

  • :exact (boolean/0) - Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

get_by_title(locator, text, options \\ [])

@spec get_by_title(
  t(),
  String.t() | Regex.t() | Playwright.RegExp.t(),
  get_by_title_options()
) :: t()

Allows locating elements by their title attribute.

Usage

Consider the following DOM structure.

<span title='Issues count'>25 issues</span>

You can check the issues count after locating it by the title text:

Page.get_by_title(page, "Issues count") |> Expect.to_have_text!("25 issues")

Since Playwright 1.27.

Arguments

  • text - Text to locate the element for.

Options

  • :exact (boolean/0) - Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

hide_highlight(locator)

@spec hide_highlight(t()) :: :ok | {:error, Playwright.error()}

Hides the element highlight previously added by Playwright.Locator.highlight/2.

Since Playwright 1.60.

hide_highlight!(locator)

@spec hide_highlight!(t()) :: :ok

Like hide_highlight/1, but returns the value and raises on error.

highlight(locator, options \\ [])

@spec highlight(t(), highlight_options()) ::
  {:ok, Playwright.Disposable.t()} | {:error, Playwright.error()}

Highlight the corresponding element(s) on the screen. Useful for debugging, don't commit the code that uses Playwright.Locator.highlight/2.

Since Playwright 1.20.

Options

  • :style (String.t/0 | %{optional(String.t()) => String.t() | number()}) - Since Playwright 1.60. Inline CSS applied to the highlight overlay, e.g.

    Locator.highlight!(locator, style: "outline: 2px dashed red")
    Locator.highlight!(locator, style: %{"color" => "red"})

highlight!(locator, options \\ [])

@spec highlight!(t(), highlight_options()) :: Playwright.Disposable.t()

Like highlight/2, but returns the value and raises on error.

hover(locator, options \\ [])

@spec hover(t(), hover_options()) :: :ok | {:error, Playwright.error()}

Hover over the matching element.

Usage

Page.get_by_role(page, :link) |> Locator.hover!()

Details

This method hovers over the element by performing the following steps:

  1. Wait for actionability checks on the element, unless :force option is set.
  2. Scroll the element into view if needed.
  3. Use Playwright.Page.mouse/1 to hover over the center of the element, or the specified :position.

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

When all steps combined have not finished during the specified :timeout, this method fails with a TimeoutError. Passing zero timeout disables this.

Since Playwright 1.14.

Options

  • :force (boolean/0) - Whether to bypass the actionability checks. Defaults to false.

  • :modifiers (list of :alt | :control | :control_or_meta | :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. "ControlOrMeta" resolves to "Control" on Windows and Linux and to "Meta" on macOS.

  • :no_wait_after (boolean/0) - Since Playwright 1.28. This option has no effect.

  • :position (map/0) - A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.

    • :x
    • :y
  • :scroll (:auto | :none) - Since Playwright 1.62. Controls whether Playwright scrolls the element into view before performing the action. Defaults to :auto, which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to :none, Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :trial (boolean/0) - When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it. Note that keyboard :modifiers will be pressed regardless of :trial to allow testing elements which are only visible when those keys are pressed.

hover!(locator, options \\ [])

@spec hover!(t(), hover_options()) :: :ok

Like hover/2, but returns the value and raises on error.

inner_html(locator, options \\ [])

@spec inner_html(t(), inner_html_options()) ::
  {:ok, String.t()} | {:error, Playwright.error()}

Returns the element.innerHTML.

Since Playwright 1.14.

Options

inner_html!(locator, options \\ [])

@spec inner_html!(t(), inner_html_options()) :: String.t()

Like inner_html/2, but returns the value and raises on error.

inner_text(locator, options \\ [])

@spec inner_text(t(), inner_text_options()) ::
  {:ok, String.t()} | {:error, Playwright.error()}

Returns the element.innerText.

Asserting text

If you need to assert text on the page, prefer Playwright.Expect.to_have_text/3 with :use_inner_text option to avoid flakiness. See assertions guide for more details.

Since Playwright 1.14.

Options

inner_text!(locator, options \\ [])

@spec inner_text!(t(), inner_text_options()) :: String.t()

Like inner_text/2, but returns the value and raises on error.

input_value(locator, options \\ [])

@spec input_value(t(), input_value_options()) ::
  {:ok, String.t()} | {:error, Playwright.error()}

Returns the value for the matching <input> or <textarea> or <select> element.

Asserting value

If you need to assert input value, prefer Playwright.Expect.to_have_value/3 to avoid flakiness. See assertions guide for more details.

Usage

value = Page.get_by_role(page, :textbox) |> Locator.input_value!()

Details

Fails for elements that are not an input, textarea or a select. However, if the element is inside the <label> element that has an associated control, returns the value of the control.

Since Playwright 1.14.

Options

input_value!(locator, options \\ [])

@spec input_value!(t(), input_value_options()) :: String.t()

Like input_value/2, but returns the value and raises on error.

is_checked(locator, options \\ [])

@spec is_checked(t(), is_checked_options()) ::
  {:ok, boolean()} | {:error, Playwright.error()}

Returns whether the element is checked. Fails if the element is not a checkbox or radio input.

Asserting checked state

If you need to assert that checkbox is checked, prefer Playwright.Expect.to_be_checked/2 to avoid flakiness. See assertions guide for more details.

Usage

checked = Page.get_by_role(page, :checkbox) |> Locator.is_checked!()

Since Playwright 1.14.

Options

is_checked!(locator, options \\ [])

@spec is_checked!(t(), is_checked_options()) :: boolean()

Like is_checked/2, but returns the value and raises on error.

is_disabled(locator, options \\ [])

@spec is_disabled(t(), is_disabled_options()) ::
  {:ok, boolean()} | {:error, Playwright.error()}

Returns whether the element is disabled, the opposite of enabled.

Asserting disabled state

If you need to assert that an element is disabled, prefer Playwright.Expect.to_be_disabled/2 to avoid flakiness. See assertions guide for more details.

Usage

disabled = Page.get_by_role(page, :button) |> Locator.is_disabled!()

Since Playwright 1.14.

Options

is_disabled!(locator, options \\ [])

@spec is_disabled!(t(), is_disabled_options()) :: boolean()

Like is_disabled/2, but returns the value and raises on error.

is_editable(locator, options \\ [])

@spec is_editable(t(), is_editable_options()) ::
  {:ok, boolean()} | {:error, Playwright.error()}

Returns whether the element is editable. If the target element is not an <input>, <textarea>, <select>, [contenteditable] and does not have a role allowing [aria-readonly], this method fails.

Asserting editable state

If you need to assert that an element is editable, prefer Playwright.Expect.to_be_editable/2 to avoid flakiness. See assertions guide for more details.

Usage

editable = Page.get_by_role(page, :textbox) |> Locator.is_editable!()

Since Playwright 1.14.

Options

is_editable!(locator, options \\ [])

@spec is_editable!(t(), is_editable_options()) :: boolean()

Like is_editable/2, but returns the value and raises on error.

is_enabled(locator, options \\ [])

@spec is_enabled(t(), is_enabled_options()) ::
  {:ok, boolean()} | {:error, Playwright.error()}

Returns whether the element is enabled.

Asserting enabled state

If you need to assert that an element is enabled, prefer Playwright.Expect.to_be_enabled/2 to avoid flakiness. See assertions guide for more details.

Usage

enabled = Page.get_by_role(page, :button) |> Locator.is_enabled!()

Since Playwright 1.14.

Options

is_enabled!(locator, options \\ [])

@spec is_enabled!(t(), is_enabled_options()) :: boolean()

Like is_enabled/2, but returns the value and raises on error.

is_hidden(locator, options \\ [])

@spec is_hidden(t(), is_hidden_options()) ::
  {:ok, boolean()} | {:error, Playwright.error()}

Returns whether the element is hidden, the opposite of visible.

Asserting visibility

If you need to assert that element is hidden, prefer Playwright.Expect.to_be_hidden/2 to avoid flakiness. See assertions guide for more details.

Usage

hidden = Page.get_by_role(page, :button) |> Locator.is_hidden!()

Since Playwright 1.14.

Options

is_hidden!(locator, options \\ [])

@spec is_hidden!(t(), is_hidden_options()) :: boolean()

Like is_hidden/2, but returns the value and raises on error.

is_visible(locator, options \\ [])

@spec is_visible(t(), is_visible_options()) ::
  {:ok, boolean()} | {:error, Playwright.error()}

Returns whether the element is visible.

Asserting visibility

If you need to assert that element is visible, prefer Playwright.Expect.to_be_visible/2 to avoid flakiness. See assertions guide for more details.

Usage

visible = Page.get_by_role(page, :button) |> Locator.is_visible!()

Since Playwright 1.14.

Options

is_visible!(locator, options \\ [])

@spec is_visible!(t(), is_visible_options()) :: boolean()

Like is_visible/2, but returns the value and raises on error.

last(locator)

@spec last(t()) :: t()

Returns locator to the last matching element.

Usage

banana = Page.get_by_role(page, :listitem) |> Locator.last()

Since Playwright 1.14.

locator(locator, selector_or_locator, options \\ [])

@spec locator(t(), String.t() | t(), locator_options()) :: t()

The method finds an element matching the specified selector in the locator's subtree. It also accepts filter options, similar to Playwright.Locator.filter/2 method.

Learn more about locators.

Since Playwright 1.14.

Arguments

  • selector_or_locator - A selector or locator to use when resolving DOM element.

Options

  • :has (Playwright.Locator.t/0) - Narrows down the results of the method to those which contain elements matching this relative locator. For example, article that has text=Playwright matches <article><div>Playwright</div></article>.

    Inner locator must be relative to the outer locator and is queried starting with the outer locator match, not the document root. For example, you can find content that has div in <article><content><div>Playwright</div></content></article>. However, looking for content that has article div will fail, because the inner locator must be relative and should not use any elements outside the content.

    Note that outer and inner locators must belong to the same frame. Inner locator must not contain FrameLocators.

  • :has_not (Playwright.Locator.t/0) - Since Playwright 1.33. Matches elements that do not contain an element that matches an inner locator. Inner locator is queried against the outer one. For example, article that does not have div matches <article><span>Playwright</span></article>.

    Note that outer and inner locators must belong to the same frame. Inner locator must not contain FrameLocators.

  • :has_not_text (String.t/0 | Regex.t/0 | Playwright.RegExp.t/0) - Since Playwright 1.33. Matches elements that do not contain specified text somewhere inside, possibly in a child or a descendant element. When passed a string, matching is case-insensitive and searches for a substring.

  • :has_text (String.t/0 | Regex.t/0 | Playwright.RegExp.t/0) - Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. When passed a string, matching is case-insensitive and searches for a substring. For example, "Playwright" matches <article><div>Playwright</div></article>.

normalize(locator)

@spec normalize(t()) :: {:ok, t()} | {:error, Playwright.error()}

Returns a new locator that uses best practices for referencing the matched element, prioritizing test ids, aria roles, and other user-facing attributes over CSS selectors. This is useful for converting implementation-detail selectors into more resilient, human-readable locators.

Usage

An agent can pick an element by its ref from an Playwright.Page.aria_snapshot/2 taken in the "ai" mode. Refs only work for the latest snapshot. Normalize the ref locator and convert it to a string to get resilient locator code in your language, the same way codegen does.

Consider the following aria snapshot.

- button "Submit" [ref=e2]

You can turn the ref into locator code:

normalized = Page.get_by_ref(page, "e2") |> Locator.normalize!()
# prints Page.get_by_role(page, :button, name: "Submit")
IO.puts(Locator.to_string(normalized))

Since Playwright 1.59.

normalize!(locator)

@spec normalize!(t()) :: t()

Like normalize/1, but returns the value and raises on error.

nth(locator, index)

@spec nth(t(), integer()) :: t()

Returns locator to the n-th matching element. It's zero based, nth(0) selects the first element.

Usage

banana = Page.get_by_role(page, :listitem) |> Locator.nth(2)

Since Playwright 1.14.

or_(locator, other_locator)

@spec or_(t(), t()) :: t()

Creates a locator matching all elements that match one or both of the two locators.

Note that when both locators match something, the resulting locator will have multiple matches, potentially causing a locator strictness violation.

Usage

Consider a scenario where you'd like to click on a "New email" button, but sometimes a security settings dialog shows up instead. In this case, you can wait for either a "New email" button, or a dialog and act accordingly.

Note

If both "New email" button and security dialog appear on screen, the "or" locator will match both of them, possibly failing with the "strict mode violation" error. In this case, you can use Playwright.Locator.first/1 to only match one of them.

new_email = Page.get_by_role(page, :button, name: "New")
dialog = Page.get_by_text(page, "Confirm security settings")
new_email |> Locator.or_(dialog) |> Locator.first() |> Expect.to_be_visible!()

if Locator.is_visible!(dialog) do
  Page.get_by_role(page, :button, name: "Dismiss") |> Locator.click!()
end

Locator.click!(new_email)

Since Playwright 1.33.

Arguments

  • other_locator - Alternative locator to match.

page(locator)

@spec page(t()) :: Playwright.Page.t()

A page this locator belongs to.

Since Playwright 1.19.

press(locator, key, options \\ [])

@spec press(t(), String.t(), press_options()) :: :ok | {:error, Playwright.error()}

Focuses the matching element and presses a combination of the keys.

Usage

Page.get_by_role(page, :textbox) |> Locator.press!("Backspace")

Details

Focuses the element, and then uses Playwright.Keyboard.down/2 and Playwright.Keyboard.up/2.

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 supported: Shift, Control, Alt, Meta, ShiftLeft, ControlOrMeta. ControlOrMeta resolves to Control on Windows and Linux and to Meta on macOS.

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", key: "Control++ or key: "Control+Shift+T" are supported as well. When specified with the modifier, modifier is pressed and being held while the subsequent key is being pressed.

Since Playwright 1.14.

Arguments

  • key - Name of the key to press or a character to generate, such as ArrowLeft or a.

Options

press!(locator, key, options \\ [])

@spec press!(t(), String.t(), press_options()) :: :ok

Like press/3, but returns the value and raises on error.

press_sequentially(locator, text, options \\ [])

@spec press_sequentially(t(), String.t(), press_sequentially_options()) ::
  :ok | {:error, Playwright.error()}

Tip

In most cases, you should use Playwright.Locator.fill/3 instead. You only need to press keys one by one if there is special keyboard handling on the page.

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 Playwright.Locator.press/3.

Usage

# Types instantly
Locator.press_sequentially!(locator, "Hello")
# Types slower, like a user
Locator.press_sequentially!(locator, "World", delay: 100)

An example of typing into a text field and then submitting the form:

locator = Page.get_by_label(page, "Password")
Locator.press_sequentially!(locator, "my password")
Locator.press!(locator, "Enter")

Since Playwright 1.38.

Arguments

  • text - String of characters to sequentially press into a focused element.

Options

press_sequentially!(locator, text, options \\ [])

@spec press_sequentially!(t(), String.t(), press_sequentially_options()) :: :ok

Like press_sequentially/3, but returns the value and raises on error.

screenshot(locator, options \\ [])

@spec screenshot(t(), screenshot_options()) ::
  {:ok, binary()} | {:error, Playwright.error()}

Take a screenshot of the element matching the locator.

Usage

Page.get_by_role(page, :link) |> Locator.screenshot!()

Disable animations and save screenshot to a file:

Page.get_by_role(page, :link) |> Locator.screenshot!(animations: :disabled, path: "link.png")

Details

This method captures a screenshot of the page, clipped to the size and position of a particular element matching the locator. If the element is covered by other elements, it will not be actually visible on the screenshot. If the element is a scrollable container, only the currently scrolled content will be visible on the 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 fails.

Returns the buffer with the captured screenshot.

Since Playwright 1.14.

Options

  • :animations (:disabled | :allow) - When set to :disabled, stops CSS animations, CSS transitions and Web Animations. Animations get different treatment depending on their duration:

    • finite animations are fast-forwarded to completion, so they'll fire transitionend event.
    • infinite animations are canceled to initial state, and then played over after the screenshot.

    Defaults to :allow that leaves animations untouched.

  • :caret (:hide | :initial) - When set to :hide, screenshot will hide text caret. When set to :initial, text caret behavior will not be changed. Defaults to :hide.

  • :mask (list of Playwright.Locator.t/0) - Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink box #FF00FF (customized by :mask_color) that completely covers its bounding box. The mask is also applied to invisible elements, see Matching only visible elements to disable that.

  • :mask_color (String.t/0) - Since Playwright 1.35. Specify the color of the overlay box for masked elements, in CSS color format. Default color is pink #FF00FF.

  • :omit_background (boolean/0) - Hides default white background and allows capturing screenshots with transparency. Not applicable to jpeg images. Defaults to false.

  • :path (String.t/0) - The file path to save the image to. The screenshot type will be inferred from file extension. If :path is a relative path, then it is resolved relative to the current working directory. If no path is provided, the image won't be saved to the disk.

  • :quality (integer/0) - The quality of the image, between 0-100. Not applicable to png images. For jpeg the default is 80. For webp, a quality of 100 (the default) produces a lossless image, while lower values use lossy compression.

  • :scale (:css | :device) - When set to :css, screenshot will have a single pixel per each css pixel on the page. For high-dpi devices, this will keep screenshots small. Using :device option will produce a single pixel per each device pixel, so screenshots of high-dpi devices will be twice as large or even larger.

    Defaults to :device.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :style (String.t/0) - Since Playwright 1.41. Text of the stylesheet to apply while making the screenshot. This is where you can hide dynamic elements, make elements invisible or change their properties to help you creating repeatable screenshots. This stylesheet pierces the Shadow DOM and applies to the inner frames.

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :type (:png | :jpeg | :webp) - Specify screenshot type, defaults to :png.

screenshot!(locator, options \\ [])

@spec screenshot!(t(), screenshot_options()) :: binary()

Like screenshot/2, but returns the value and raises on error.

scroll_into_view_if_needed(locator, options \\ [])

@spec scroll_into_view_if_needed(t(), scroll_into_view_if_needed_options()) ::
  :ok | {:error, Playwright.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.

See scrolling for alternative ways to scroll.

Since Playwright 1.14.

Options

scroll_into_view_if_needed!(locator, options \\ [])

@spec scroll_into_view_if_needed!(t(), scroll_into_view_if_needed_options()) :: :ok

Like scroll_into_view_if_needed/2, but returns the value and raises on error.

select_option(locator, values, options \\ [])

@spec select_option(
  t(),
  :null
  | String.t()
  | Playwright.ElementHandle.t()
  | [String.t()]
  | %{
      optional(:value) => String.t(),
      optional(:label) => String.t(),
      optional(:index) => integer(),
      optional(:valueOrLabel) => String.t()
    }
  | [Playwright.ElementHandle.t()]
  | [
      %{
        optional(:value) => String.t(),
        optional(:label) => String.t(),
        optional(:index) => integer(),
        optional(:valueOrLabel) => String.t()
      }
    ],
  select_option_options()
) :: {:ok, [String.t()]} | {:error, Playwright.error()}

Selects option or options in <select>.

Details

This method waits for actionability checks, waits until all specified options are present in the <select> element and selects these options.

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

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

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

Usage

<select multiple>
  <option value="red">Red</option>
  <option value="green">Green</option>
  <option value="blue">Blue</option>
</select>
# single selection matching the value or label
Locator.select_option!(element, "blue")

# single selection matching the label
Locator.select_option!(element, %{label: "Blue"})

# multiple selection for red, green and blue options
Locator.select_option!(element, ["red", "green", "blue"])

Since Playwright 1.14.

Arguments

  • values - Options to select. 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 matching both values and labels. Option is considered matching if all specified properties match.
    • :value - Matches by option.value. Optional.
    • :label - Matches by option.label. Optional.
    • :index - Matches by the index. Optional.
    • :valueOrLabel - Matches by value or label, as a string does. Combines such a match with index in one list.

Options

select_option!(locator, values, options \\ [])

@spec select_option!(
  t(),
  :null
  | String.t()
  | Playwright.ElementHandle.t()
  | [String.t()]
  | %{
      optional(:value) => String.t(),
      optional(:label) => String.t(),
      optional(:index) => integer(),
      optional(:valueOrLabel) => String.t()
    }
  | [Playwright.ElementHandle.t()]
  | [
      %{
        optional(:value) => String.t(),
        optional(:label) => String.t(),
        optional(:index) => integer(),
        optional(:valueOrLabel) => String.t()
      }
    ],
  select_option_options()
) :: [String.t()]

Like select_option/3, but returns the value and raises on error.

select_text(locator, options \\ [])

@spec select_text(t(), select_text_options()) :: :ok | {:error, Playwright.error()}

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

If the element is inside the <label> element that has an associated control, focuses and selects text in the control instead.

Since Playwright 1.14.

Options

select_text!(locator, options \\ [])

@spec select_text!(t(), select_text_options()) :: :ok

Like select_text/2, but returns the value and raises on error.

set_checked(locator, checked, options \\ [])

@spec set_checked(t(), boolean(), set_checked_options()) ::
  :ok | {:error, Playwright.error()}

Set the state of a checkbox or a radio element.

Usage

Page.get_by_role(page, :checkbox) |> Locator.set_checked!(true)

Details

This method checks or unchecks an element by performing the following steps:

  1. Ensure that matched element is a checkbox or a radio input. If not, this method fails.
  2. If the element already has the right checked state, this method returns immediately.
  3. Wait for actionability checks on the matched element, unless :force option is set. If the element is detached during the checks, the whole action is retried.
  4. Scroll the element into view if needed.
  5. Use Playwright.Page.mouse/1 to click in the center of the element.
  6. Ensure that the element is now checked or unchecked. If not, this method fails.

When all steps combined have not finished during the specified :timeout, this method fails with a TimeoutError. Passing zero timeout disables this.

Since Playwright 1.15.

Arguments

  • checked - Whether to check or uncheck the checkbox.

Options

  • :force (boolean/0) - Whether to bypass the actionability checks. Defaults to false.

  • :no_wait_after (boolean/0) - This option has no effect.

  • :position (map/0) - A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.

    • :x
    • :y
  • :scroll (:auto | :none) - Since Playwright 1.62. Controls whether Playwright scrolls the element into view before performing the action. Defaults to :auto, which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to :none, Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :trial (boolean/0) - When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it.

set_checked!(locator, checked, options \\ [])

@spec set_checked!(t(), boolean(), set_checked_options()) :: :ok

Like set_checked/3, but returns the value and raises on error.

set_input_files(locator, files, options \\ [])

@spec set_input_files(
  t(),
  String.t()
  | [String.t()]
  | %{name: String.t(), mimeType: String.t(), buffer: binary()}
  | [%{name: String.t(), mimeType: String.t(), buffer: binary()}],
  set_input_files_options()
) :: :ok | {:error, Playwright.error()}

Upload file or multiple files into <input type=file>. For inputs with a [webkitdirectory] attribute, only a single directory path is supported.

Usage

# Select one file
Page.get_by_label(page, "Upload file") |> Locator.set_input_files!(Path.join(__DIR__, "myfile.pdf"))

# Select multiple files
Page.get_by_label(page, "Upload files")
|> Locator.set_input_files!([Path.join(__DIR__, "file1.txt"), Path.join(__DIR__, "file2.txt")])

# Select a directory
Page.get_by_label(page, "Upload directory") |> Locator.set_input_files!(Path.join(__DIR__, "mydir"))

# Remove all the selected files
Page.get_by_label(page, "Upload file") |> Locator.set_input_files!([])

# Upload buffer from memory
Page.get_by_label(page, "Upload file")
|> Locator.set_input_files!(%{name: "file.txt", mimeType: "text/plain", buffer: "this is test"})

Details

Sets the value of the file input to these file paths or files. If some of the files are relative paths, then they are resolved relative to the current working directory. For empty array, clears the selected files.

This method expects Locator to point to an input element. However, if the element is inside the <label> element that has an associated control, targets the control instead.

Since Playwright 1.14.

Arguments

  • files
    • :name - File name
    • :mimeType - File type
    • :buffer - File content

Options

set_input_files!(locator, files, options \\ [])

@spec set_input_files!(
  t(),
  String.t()
  | [String.t()]
  | %{name: String.t(), mimeType: String.t(), buffer: binary()}
  | [%{name: String.t(), mimeType: String.t(), buffer: binary()}],
  set_input_files_options()
) :: :ok

Like set_input_files/3, but returns the value and raises on error.

tap(locator, options \\ [])

@spec tap(t(), tap_options()) :: :ok | {:error, Playwright.error()}

Perform a tap gesture on the element matching the locator. For examples of emulating other gestures by manually dispatching touch events, see the emulating legacy touch events page.

Details

This method taps the element by performing the following steps:

  1. Wait for actionability checks on the element, unless :force option is set.
  2. Scroll the element into view if needed.
  3. Use Playwright.Page.touchscreen/1 to tap the center of the element, or the specified :position.

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

When all steps combined have not finished during the specified :timeout, this method fails with a TimeoutError. Passing zero timeout disables this.

Note

Playwright.Locator.tap/2 requires that the :has_touch option of the browser context be set to true.

Since Playwright 1.14.

Options

  • :force (boolean/0) - Whether to bypass the actionability checks. Defaults to false.

  • :modifiers (list of :alt | :control | :control_or_meta | :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. "ControlOrMeta" resolves to "Control" on Windows and Linux and to "Meta" on macOS.

  • :no_wait_after (boolean/0) - This option has no effect.

  • :position (map/0) - A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.

    • :x
    • :y
  • :scroll (:auto | :none) - Since Playwright 1.62. Controls whether Playwright scrolls the element into view before performing the action. Defaults to :auto, which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to :none, Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :trial (boolean/0) - When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it. Note that keyboard :modifiers will be pressed regardless of :trial to allow testing elements which are only visible when those keys are pressed.

tap!(locator, options \\ [])

@spec tap!(t(), tap_options()) :: :ok

Like tap/2, but returns the value and raises on error.

text_content(locator, options \\ [])

@spec text_content(t(), text_content_options()) ::
  {:ok, nil | String.t()} | {:error, Playwright.error()}

Returns the node.textContent.

Asserting text

If you need to assert text on the page, prefer Playwright.Expect.to_have_text/3 to avoid flakiness. See assertions guide for more details.

Since Playwright 1.14.

Options

text_content!(locator, options \\ [])

@spec text_content!(t(), text_content_options()) :: nil | String.t()

Like text_content/2, but returns the value and raises on error.

to_string(locator)

@spec to_string(t()) :: String.t()

Returns a human-readable representation of the locator, using the Playwright.Locator.description/1 if one exists; otherwise, it generates a string based on the locator's selector.

Since Playwright 1.57.

type(locator, text, options \\ [])

This function is deprecated. In most cases, you should use Playwright.Locator.fill/3 instead. You only need to press keys one by one if there is special keyboard handling on the page - in this case use Playwright.Locator.press_sequentially/3.
@spec type(t(), String.t(), type_options()) :: :ok | {:error, Playwright.error()}

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 Playwright.Locator.press/3.

Usage

Since Playwright 1.14.

Arguments

  • text - A text to type into a focused element.

Options

type!(locator, text, options \\ [])

This function is deprecated. In most cases, you should use Playwright.Locator.fill/3 instead. You only need to press keys one by one if there is special keyboard handling on the page - in this case use Playwright.Locator.press_sequentially/3.
@spec type!(t(), String.t(), type_options()) :: :ok

Like type/3, but returns the value and raises on error.

uncheck(locator, options \\ [])

@spec uncheck(t(), uncheck_options()) :: :ok | {:error, Playwright.error()}

Ensure that checkbox or radio element is unchecked.

Usage

Page.get_by_role(page, :checkbox) |> Locator.uncheck!()

Details

This method unchecks the element by performing the following steps:

  1. Ensure that element is a checkbox or a radio input. If not, this method fails. If the element is already unchecked, 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/1 to click in the center of the element.
  5. Ensure that the element is now unchecked. If not, this method fails.

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

When all steps combined have not finished during the specified :timeout, this method fails with a TimeoutError. Passing zero timeout disables this.

Since Playwright 1.14.

Options

  • :force (boolean/0) - Whether to bypass the actionability checks. Defaults to false.

  • :no_wait_after (boolean/0) - This option has no effect.

  • :position (map/0) - A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.

    • :x
    • :y
  • :scroll (:auto | :none) - Since Playwright 1.62. Controls whether Playwright scrolls the element into view before performing the action. Defaults to :auto, which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to :none, Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling.

  • :signal (Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Maximum time in milliseconds. Defaults to 30000 (30 seconds). 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 methods.

    In Elixir, :infinity is the same as 0: no timeout.

  • :trial (boolean/0) - When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it.

uncheck!(locator, options \\ [])

@spec uncheck!(t(), uncheck_options()) :: :ok

Like uncheck/2, but returns the value and raises on error.

visible(locator)

@spec visible(t()) :: t()

Returns a locator that matches only visible elements, ignoring the invisible ones. This is the recommended way to distinguish elements by visibility, as opposed to the :visible CSS pseudo-class.

Note that visibility is checked every time the locator is used, and not at the moment of the Playwright.Locator.visible/1 call.

Usage

Consider a page with two buttons, the first invisible and the second visible.

<button style='display: none'>Invisible</button>
<button>Visible</button>

This will only find the second button, because it is visible, and then click it.

Page.locator(page, "button") |> Locator.visible() |> Locator.click!()

To match invisible elements instead, use Playwright.Locator.filter/2 with the visible option set to false.

Since Playwright 1.63.

wait_for(locator, options \\ [])

@spec wait_for(t(), wait_for_options()) :: :ok | {:error, Playwright.error()}

Returns when element specified by locator satisfies the :state option.

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

Usage

order_sent = Page.locator(page, "#order-sent")
Locator.wait_for!(order_sent)

Since Playwright 1.16.

Options

wait_for!(locator, options \\ [])

@spec wait_for!(t(), wait_for_options()) :: :ok

Like wait_for/2, but returns the value and raises on error.

wait_for_function(locator, expression, arg \\ nil, options \\ [])

@spec wait_for_function(t(), String.t(), term() | nil, wait_for_function_options()) ::
  :ok | {:error, Playwright.error()}

Returns when expression returns a truthy value, called with the matching element as a first argument, and arg as a second argument.

This is a generic way to wait for an element to reach a custom condition without asserting it. The locator is re-resolved on each retry, so it tolerates the element being re-rendered while waiting.

If expression returns a Promise, this method will wait for the promise to resolve before checking its value.

If expression throws or rejects, this method fails.

Usage

Wait for an attribute to appear:

toggle = Page.get_by_role(page, :button, name: "Menu")
Locator.click!(toggle)
Locator.wait_for_function!(toggle, "element => element.hasAttribute('aria-expanded')")

Passing argument to expression:

Page.get_by_test_id(page, "status")
|> Locator.wait_for_function!("(element, value) => element.textContent === value", "Ready")

Since Playwright 1.62.

Arguments

  • expression - Function to be evaluated in the page context.
  • arg - Optional argument to pass to expression.

Options

wait_for_function!(locator, expression, arg \\ nil, options \\ [])

@spec wait_for_function!(t(), String.t(), term() | nil, wait_for_function_options()) ::
  :ok

Like wait_for_function/4, but returns the value and raises on error.

within(locator, other_locator)

@spec within(t(), t()) :: t()

Returns a locator that matches this locator's elements inside each element matched by other_locator.

Note that relative locators, such as Playwright.Locator.nth/2 or Playwright.Locator.first/1, are resolved separately inside each matched parent. In the example below, Page.get_by_role(page, :cell) |> Locator.nth(2) picks the third cell of every row, not the third cell in the whole table.

Usage

third_column = Page.get_by_role(page, :cell) |> Locator.nth(2) |> Locator.within(Page.get_by_role(page, :row))
Expect.to_have_text!(third_column, ["Apple", "Banana", "Cherry"])

Since Playwright 1.64.

Arguments

  • other_locator - Locator matching the parent elements to search within.