Locator

Struct Locator 

Source
pub struct Locator<'a> { /* private fields */ }
Expand description

A locator for finding elements on a page.

Locators are lightweight handles that store selection criteria. They don’t query the DOM until an action is performed, enabling auto-waiting.

Implementations§

Source§

impl Locator<'_>

Source

pub async fn click(&self) -> Result<(), LocatorError>

Click the element.

Waits for the element to be visible and enabled, then clicks its center.

§Errors

Returns an error if:

  • The element is not found within the timeout
  • The element is not visible
  • The CDP command fails
§Panics

Panics if a visible element lacks bounding box coordinates. This should never occur as wait_for_actionable ensures visibility before returning.

Source

pub async fn dblclick(&self) -> Result<(), LocatorError>

Double-click the element.

§Errors

Returns an error if the element cannot be clicked.

§Panics

Panics if a visible element lacks bounding box coordinates. This should never occur as wait_for_actionable ensures visibility before returning.

Source

pub async fn fill(&self, text: &str) -> Result<(), LocatorError>

Fill the element with text (clears existing content first).

This is for input and textarea elements.

§Errors

Returns an error if the element cannot be focused or text cannot be inserted.

Source

pub async fn type_text(&self, text: &str) -> Result<(), LocatorError>

Type text character by character.

Unlike fill, this types each character with keydown/keyup events.

§Errors

Returns an error if the element cannot be focused or keys cannot be dispatched.

Source

pub async fn press(&self, key: &str) -> Result<(), LocatorError>

Press a key or key combination.

Examples: “Enter”, “Backspace”, “Control+a”, “Shift+Tab”

§Errors

Returns an error if the element cannot be focused or the key cannot be pressed.

Source

pub async fn hover(&self) -> Result<(), LocatorError>

Hover over the element.

§Errors

Returns an error if the element cannot be found or the mouse event fails.

§Panics

Panics if a visible element lacks bounding box coordinates. This should never occur as wait_for_actionable ensures visibility before returning.

Source

pub async fn focus(&self) -> Result<(), LocatorError>

Focus the element.

§Errors

Returns an error if the element cannot be found or focused.

Source

pub async fn clear(&self) -> Result<(), LocatorError>

Clear the element’s content.

§Errors

Returns an error if the element cannot be cleared.

Source

pub async fn check(&self) -> Result<(), LocatorError>

Check a checkbox or radio button.

§Errors

Returns an error if the element cannot be checked.

Source

pub async fn uncheck(&self) -> Result<(), LocatorError>

Uncheck a checkbox.

§Errors

Returns an error if the element cannot be unchecked.

Source

pub async fn select_option(&self, option: &str) -> Result<(), LocatorError>

Select an option in a <select> element by value, label, or index.

§Arguments
  • option - The option to select. Can be:
    • A string value matching the option’s value attribute
    • A string matching the option’s visible text
§Errors

Returns an error if the element is not a select or the option is not found.

§Example
// Select by value
page.locator("select#size").select_option("medium").await?;

// Select by visible text
page.locator("select#size").select_option("Medium Size").await?;
Source

pub async fn select_options(&self, options: &[&str]) -> Result<(), LocatorError>

Select multiple options in a <select multiple> element.

§Arguments
  • options - A slice of option values or labels to select.
§Errors

Returns an error if the element is not a multi-select or options are not found.

Source

pub async fn text_content(&self) -> Result<Option<String>, LocatorError>

Get the text content of the element.

§Errors

Returns an error if the element cannot be queried.

Source

pub async fn is_visible(&self) -> Result<bool, LocatorError>

Check if the element is visible.

§Errors

Returns an error if the element cannot be queried.

Source

pub async fn is_checked(&self) -> Result<bool, LocatorError>

Check if the element is checked (for checkboxes/radios).

§Errors

Returns an error if the element cannot be queried.

Source

pub async fn count(&self) -> Result<usize, LocatorError>

Count matching elements.

§Errors

Returns an error if the elements cannot be queried.

Source§

impl<'a> Locator<'a>

Source

pub fn page(&self) -> &'a Page

Get the page this locator belongs to.

Source

pub fn selector(&self) -> &Selector

Get the selector.

Source

pub fn options(&self) -> &LocatorOptions

Get the options.

Source

pub fn timeout(self, timeout: Duration) -> Self

Set a custom timeout for this locator.

Source

pub fn locator(&self, selector: impl Into<String>) -> Locator<'a>

Create a child locator that further filters elements.

§Example
let items = page.locator(".list").locator(".item");
Source

pub fn first(&self) -> Locator<'a>

Select the first matching element.

Source

pub fn last(&self) -> Locator<'a>

Select the last matching element.

Source

pub fn nth(&self, index: i32) -> Locator<'a>

Select the nth matching element (0-indexed).

Trait Implementations§

Source§

impl<'a> Clone for Locator<'a>

Source§

fn clone(&self) -> Locator<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Locator<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<RoleLocatorBuilder<'a>> for Locator<'a>

Source§

fn from(builder: RoleLocatorBuilder<'a>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> Freeze for Locator<'a>

§

impl<'a> !RefUnwindSafe for Locator<'a>

§

impl<'a> Send for Locator<'a>

§

impl<'a> Sync for Locator<'a>

§

impl<'a> Unpin for Locator<'a>

§

impl<'a> !UnwindSafe for Locator<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more