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<'_>
impl Locator<'_>
Sourcepub async fn click(&self) -> Result<(), LocatorError>
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.
Sourcepub async fn dblclick(&self) -> Result<(), LocatorError>
pub async fn dblclick(&self) -> Result<(), LocatorError>
Sourcepub async fn fill(&self, text: &str) -> Result<(), LocatorError>
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.
Sourcepub async fn type_text(&self, text: &str) -> Result<(), LocatorError>
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.
Sourcepub async fn press(&self, key: &str) -> Result<(), LocatorError>
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.
Sourcepub async fn hover(&self) -> Result<(), LocatorError>
pub async fn hover(&self) -> Result<(), LocatorError>
Sourcepub async fn focus(&self) -> Result<(), LocatorError>
pub async fn focus(&self) -> Result<(), LocatorError>
Sourcepub async fn clear(&self) -> Result<(), LocatorError>
pub async fn clear(&self) -> Result<(), LocatorError>
Sourcepub async fn check(&self) -> Result<(), LocatorError>
pub async fn check(&self) -> Result<(), LocatorError>
Sourcepub async fn uncheck(&self) -> Result<(), LocatorError>
pub async fn uncheck(&self) -> Result<(), LocatorError>
Sourcepub async fn select_option(&self, option: &str) -> Result<(), LocatorError>
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
valueattribute - A string matching the option’s visible text
- A string value matching the option’s
§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?;Sourcepub async fn select_options(&self, options: &[&str]) -> Result<(), LocatorError>
pub async fn select_options(&self, options: &[&str]) -> Result<(), LocatorError>
Sourcepub async fn text_content(&self) -> Result<Option<String>, LocatorError>
pub async fn text_content(&self) -> Result<Option<String>, LocatorError>
Sourcepub async fn is_visible(&self) -> Result<bool, LocatorError>
pub async fn is_visible(&self) -> Result<bool, LocatorError>
Sourcepub async fn is_checked(&self) -> Result<bool, LocatorError>
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.