You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rewrite to make its use more understandable + remove getSelector (#6235)
* Rewrite to make its use more understandable + remove getSelector
* Add more examples + simplify
* lint
* add note about uniqueness of selector
* lint
description: 'The Element Selector exposes APIs that enable you to change the default selector strategy and override the selectors that are returned per element.'
3
+
description: 'Customize how Cypress chooses selectors in Studio and Selector Playground by setting your preferred selector strategy.'
4
4
sidebar_label: ElementSelector
5
5
sidebar_position: 105
6
6
---
@@ -9,13 +9,18 @@ sidebar_position: 105
9
9
10
10
# Cypress.ElementSelector
11
11
12
-
The Element Selector API is used to get the selector priority for selecting elements in [Cypress Studio](/app/guides/cypress-studio) and [Selector Playground](/app/core-concepts/open-mode#Selector-Playground).
12
+
The ElementSelector API lets you define how Cypress selects elements in tools like [Cypress Studio](/app/guides/cypress-studio) and the [Selector Playground](/app/core-concepts/open-mode#Selector-Playground).
13
+
14
+
By setting your own selector strategy, you can control which attributes Cypress prioritizes (like `data-*`, `id`, or `aria-label`) when generating selectors. This helps you enforce consistency, improve test readability, and make generated tests more resilient to changes in your HTML.
15
+
16
+
Cypress uses a strategy to generate selectors that are not only based on your preferred selectors, but also guaranteed to be **unique** within the document.
17
+
18
+
This means Cypress will **attempt to follow your configured `selectorPriority`**, but may skip lower-priority options or combine multiple selectors if a single attribute isn't unique enough.
With the default selector priority, Cypress prioritizes `id`, so the selector would be `#submit-btn`.
58
+
44
59
<Iconname="angle-right" /> **$el _(Object)_**
45
60
46
-
The [jQuery element](http://api.jquery.com/Types/#jQuery) that you want to get
47
-
the selector value for.
61
+
The [jQuery element](http://api.jquery.com/Types/#jQuery) for which you want to retrieve a selector.
48
62
49
63
## Examples
50
64
51
-
### Selector Priority
65
+
### Set custom selector priority
52
66
53
-
Set the selector priority to favor role, then aria-label, then name, then classes, then attributes.
67
+
You can customize how Cypress generates selectors by defining a priority order for which attributes to prefer. This affects the selectors you see in tools like [Cypress Studio](/app/guides/cypress-studio) and the [Selector Playground](/app/core-concepts/open-mode#Selector-Playground).
68
+
69
+
For example, this config tells Cypress to prefer semantic and accessibility attributes before falling back to styling details like class names.
In multilingual applications, selectors based on text or labels may change between locales. Prefer stable, language-agnostic attributes like `data-*`, `role`, and `aria-labelledby`.
0 commit comments