LocatorScope Class

Creates the locators of the elements of a page, of a component or of the elements of another locator, as Playwright does. A locator finds its elements again each time it is used, so it keeps working when the page renders new elements. The texts are matched as substrings, ignoring the case and collapsing the white space, unless they are exact. The elements of the open shadow roots are found too.

public abstract class LocatorScope

Methods

NameDescription
GetByAltText(string, bool)Creates a locator of the elements (usually images) whose alt attribute contains a text.
GetByAltText(Regex)Creates a locator of the elements whose alt attribute matches a regular expression.
GetByLabel(string, bool)Creates a locator of the elements labelled by a text: the elements referenced by aria-labelledby, else the aria-label attribute, else the label elements of a form control.
GetByLabel(Regex)Creates a locator of the elements whose label matches a regular expression.
GetByPlaceholder(string, bool)Creates a locator of the elements whose placeholder attribute contains a text.
GetByPlaceholder(Regex)Creates a locator of the elements whose placeholder attribute matches a regular expression.
GetByRole(string, string?, GetByRoleOptions?)Creates a locator of the elements that have an ARIA role and, optionally, an accessible name (GetByRole("button", "Save")). The elements hidden from the accessibility tree are not found, unless GetByRoleOptions.IncludeHidden is true.
GetByRole(string, Regex, GetByRoleOptions?)Creates a locator of the elements that have an ARIA role and an accessible name that matches a regular expression.
GetByTestId(string)Creates a locator of the elements whose test identifier is a value: the data-testid attribute by default (see TestPageSettings.TestIdAttribute). The value is compared as is.
GetByTestId(Regex)Creates a locator of the elements whose test identifier matches a regular expression.
GetByText(string, bool)Creates a locator of the smallest elements that contain a text: an element is found when its text matches, and the text of none of its children matches. The text of an input button is its value.
GetByText(Regex)Creates a locator of the smallest elements whose text matches a regular expression.
GetByTitle(string, bool)Creates a locator of the elements whose title attribute contains a text.
GetByTitle(Regex)Creates a locator of the elements whose title attribute matches a regular expression.
Locator(string)Creates a locator of the elements that match a CSS selector.

GetByAltText overloads

GetByAltText(string, bool)

LocatorScope.cs

Creates a locator of the elements (usually images) whose alt attribute contains a text.

public Locator GetByAltText(string text, bool exact = false)
Parameters
text string
The text.
exact bool
Whether the text matches the whole attribute, with the case. By default, it matches a substring, ignoring the case.
Returns

Locator The locator.

GetByAltText(Regex)

LocatorScope.cs

Creates a locator of the elements whose alt attribute matches a regular expression.

public Locator GetByAltText(Regex text)
Parameters
text Regex
The regular expression.
Returns

Locator The locator.

GetByLabel overloads

GetByLabel(string, bool)

LocatorScope.cs

Creates a locator of the elements labelled by a text: the elements referenced by aria-labelledby, else the aria-label attribute, else the label elements of a form control.

public Locator GetByLabel(string text, bool exact = false)
Parameters
text string
The text of the label.
exact bool
Whether the text matches the whole label, with the case. By default, it matches a substring, ignoring the case.
Returns

Locator The locator.

GetByLabel(Regex)

LocatorScope.cs

Creates a locator of the elements whose label matches a regular expression.

public Locator GetByLabel(Regex text)
Parameters
text Regex
The regular expression.
Returns

Locator The locator.

GetByPlaceholder overloads

GetByPlaceholder(string, bool)

LocatorScope.cs

Creates a locator of the elements whose placeholder attribute contains a text.

public Locator GetByPlaceholder(string text, bool exact = false)
Parameters
text string
The text.
exact bool
Whether the text matches the whole attribute, with the case. By default, it matches a substring, ignoring the case.
Returns

Locator The locator.

GetByPlaceholder(Regex)

LocatorScope.cs

Creates a locator of the elements whose placeholder attribute matches a regular expression.

public Locator GetByPlaceholder(Regex text)
Parameters
text Regex
The regular expression.
Returns

Locator The locator.

GetByRole overloads

GetByRole(string, string?, GetByRoleOptions?)

LocatorScope.cs

Creates a locator of the elements that have an ARIA role and, optionally, an accessible name (GetByRole("button", "Save")). The elements hidden from the accessibility tree are not found, unless GetByRoleOptions.IncludeHidden is true.

public Locator GetByRole(string role, string? name = null, GetByRoleOptions? options = null)
Parameters
role string
The computed ARIA role (button, link, textbox, heading...).
name string?
A substring of the accessible name, ignoring the case (see GetByRoleOptions.Exact), or null to match any name.
options GetByRoleOptions?
The states of the elements, or null to match any state.
Returns

Locator The locator.

GetByRole(string, Regex, GetByRoleOptions?)

LocatorScope.cs

Creates a locator of the elements that have an ARIA role and an accessible name that matches a regular expression.

public Locator GetByRole(string role, Regex name, GetByRoleOptions? options = null)
Parameters
role string
The computed ARIA role.
name Regex
The regular expression.
options GetByRoleOptions?
The states of the elements, or null to match any state.
Returns

Locator The locator.

GetByTestId overloads

GetByTestId(string)

LocatorScope.cs

Creates a locator of the elements whose test identifier is a value: the data-testid attribute by default (see TestPageSettings.TestIdAttribute). The value is compared as is.

public Locator GetByTestId(string testId)
Parameters
testId string
The test identifier.
Returns

Locator The locator.

GetByTestId(Regex)

LocatorScope.cs

Creates a locator of the elements whose test identifier matches a regular expression.

public Locator GetByTestId(Regex testId)
Parameters
testId Regex
The regular expression.
Returns

Locator The locator.

GetByText overloads

GetByText(string, bool)

LocatorScope.cs

Creates a locator of the smallest elements that contain a text: an element is found when its text matches, and the text of none of its children matches. The text of an input button is its value.

public Locator GetByText(string text, bool exact = false)
Parameters
text string
The text.
exact bool
Whether the text matches the whole text, with the case. By default, it matches a substring, ignoring the case.
Returns

Locator The locator.

GetByText(Regex)

LocatorScope.cs

Creates a locator of the smallest elements whose text matches a regular expression.

public Locator GetByText(Regex text)
Parameters
text Regex
The regular expression.
Returns

Locator The locator.

GetByTitle overloads

GetByTitle(string, bool)

LocatorScope.cs

Creates a locator of the elements whose title attribute contains a text.

public Locator GetByTitle(string text, bool exact = false)
Parameters
text string
The text.
exact bool
Whether the text matches the whole attribute, with the case. By default, it matches a substring, ignoring the case.
Returns

Locator The locator.

GetByTitle(Regex)

LocatorScope.cs

Creates a locator of the elements whose title attribute matches a regular expression.

public Locator GetByTitle(Regex text)
Parameters
text Regex
The regular expression.
Returns

Locator The locator.

Locator(string)

LocatorScope.cs

Creates a locator of the elements that match a CSS selector.

public Locator Locator(string selector)

Parameters

selector string
The CSS selector.

Returns

Locator The locator.