The `:visible` pseudo-class in CSS selectors matches the elements that are
[visible](./actionability.md#visible). For example, `input` matches all the inputs on the page, while
`input:visible` matches only visible inputs. This is useful to distinguish elements that are very
similar but differ in visibility.
:::note
It's usually better to follow the [best practices](#best-practices) and find a more reliable way to
uniquely identify the element.
:::
Consider a page with two buttons, first invisible and second visible.
```html
<buttonstyle='display: none'>Invisible</button>
<button>Visible</button>
```
* This will find the first button, because it is the first one in DOM order. Then it will wait for the button to become visible before clicking, or timeout while waiting:
The `:has()` pseudo-class is an [experimental CSS pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:has). It returns an element if any of the selectors passed as parameters
relative to the :scope of the given element match at least one element.
The `:text` pseudo-class matches elements that have a text node child with specific text.
It is similar to the [text] engine, but can be used in combination with other `css` selector extensions.
There are a few variations that support different arguments:
*`:text("substring")` - Matches when element's text contains "substring" somewhere. Matching is case-insensitive. Matching also normalizes whitespace, for example it turns multiple spaces into one, trusn line breaks into spaces and ignores leading and trailing whitespace.
*`:text-is("string")` - Matches when element's text equals the "string". Matching is case-insensitive and normalizes whitespace.
*`button:text("Sign in")` - Text selector may be combined with regular CSS.
*`:text-matches("[+-]?\\d+")` - Matches text against a regular expression. Note that special characters like back-slash `\`, quotes `"`, square brackets `[]` and more should be escaped. Learn more about [regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp).
*`:text-matches("value", "i")` - Matches text against a regular expression with specified flags.
- Both `"css=article div"` and `"css:light=article div"` match the first `<div>In the light dom</div>`.
- Both `"css=article > div"` and `"css:light=article > div"` match two `div` elements that are direct children of the `article`.
-`"css=article .in-the-shadow"` matches the `<div class='in-the-shadow'>`, piercing the shadow root, while `"css:light=article .in-the-shadow"` does not match anything.
-`"css:light=article div > span"` does not match anything, because both light-dom `div` elements do not contain a `span`.
-`"css=article div > span"` matches the `<span class='content'>`, piercing the shadow root.
-`"css=article > .in-the-shadow"` does not match anything, because `<div class='in-the-shadow'>` is not a direct child of `article`
-`"css:light=article > .in-the-shadow"` does not match anything.
-`"css=article li#target"` matches the `<li id='target'>Deep in the shadow</li>`, piercing two shadow roots.
Attribute engines are selecting based on the corresponding attribute value. For example: `data-test-id=foo` is equivalent to `css=[data-test-id="foo"]`, and `id:light=foo` is equivalent to `css:light=[id="foo"]`.
Selectors defined as `engine=body` or in short-form can be combined with the `>>` token, e.g. `selector1 >> selector2 >> selectors3`. When selectors are chained, next one is queried relative to the previous one's result.
If a selector needs to include `>>` in the body, it should be escaped inside a string to not be confused with chaining separator, e.g. `text="some >> text"`.
By default, chained selectors resolve to an element queried by the last selector. A selector can be prefixed with `*` to capture elements that are queried by an intermediate selector.
For example, `css=article >> text=Hello` captures the element with the text `Hello`, and `*css=article >> text=Hello` (note the `*`) captures the `article` element that contains some element with the text `Hello`.
The choice of selectors determines the resiliency of automation scripts. To reduce the maintenance burden, we recommend prioritizing user-facing attributes and explicit contracts.
### Prioritize user-facing attributes
Attributes like text content, input placeholder, accessibility roles and labels are user-facing attributes that change rarely. These attributes are not impacted by DOM structure changes.
The following examples use the built-in [text] and [css] selector engines.
When user-facing attributes change frequently, it is recommended to use explicit test ids, like `data-test-id`. These `data-*` attributes are supported by the [css] and [id selectors][id].