|
| 1 | +--- |
| 2 | +title: 'Cypress.ElementSelector | Cypress Documentation' |
| 3 | +description: 'Customize how Cypress chooses selectors in Studio and Selector Playground by setting your preferred selector strategy.' |
| 4 | +sidebar_label: ElementSelector |
| 5 | +sidebar_position: 105 |
| 6 | +--- |
| 7 | + |
| 8 | +<ProductHeading product="app" /> |
| 9 | + |
| 10 | +# Cypress.ElementSelector |
| 11 | + |
| 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) (which will be replaced by Cypress Studio once it is no longer experimental). |
| 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. |
| 19 | + |
| 20 | +## Syntax |
| 21 | + |
| 22 | +```javascript |
| 23 | +Cypress.ElementSelector.defaults(options) |
| 24 | +``` |
| 25 | + |
| 26 | +### Arguments |
| 27 | + |
| 28 | +<Icon name="angle-right" /> **options _(Object)_** |
| 29 | + |
| 30 | +An object containing any or all of the following options: |
| 31 | + |
| 32 | +| Option | Accepts | Description | |
| 33 | +| ------------------ | ------------------ | ---------------------------------------------------------------------- | |
| 34 | +| `selectorPriority` | `Array of strings` | Determines the order of attributes Cypress uses to generate selectors. | |
| 35 | + |
| 36 | +:::info |
| 37 | + |
| 38 | +##### API Stability |
| 39 | + |
| 40 | +The `selectorPriority` API is under active development and may change in future versions as we refine the best way to generate selectors within our products. Stay updated with our [changelog](/app/references/changelog) for any breaking changes. |
| 41 | + |
| 42 | +::: |
| 43 | + |
| 44 | +Accepted values for `selectorPriority` are: |
| 45 | + |
| 46 | +- `attribute:${string}` - for specific attributes like `attribute:aria-label`, `attribute:lang`, etc. |
| 47 | +- `attributes` - general fallback for any other attributes |
| 48 | +- `class` |
| 49 | +- `data-${string}` - for specific data attributes like `data-cy`, `data-testid`, etc. |
| 50 | +- `id` |
| 51 | +- `name` |
| 52 | +- `nth-child` |
| 53 | +- `tag` |
| 54 | + |
| 55 | +<DefaultSelectorPriority /> |
| 56 | + |
| 57 | +Consider the following HTML: |
| 58 | + |
| 59 | +```html |
| 60 | +<button id="submit-btn" class="primary" role="button" aria-label="Submit"> |
| 61 | + Submit |
| 62 | +</button> |
| 63 | +``` |
| 64 | + |
| 65 | +With the default selector priority, Cypress prioritizes `id`, so the selector would be `#submit-btn`. |
| 66 | + |
| 67 | +<Icon name="angle-right" /> **$el _(Object)_** |
| 68 | + |
| 69 | +The [jQuery element](http://api.jquery.com/Types/#jQuery) for which you want to retrieve a selector. |
| 70 | + |
| 71 | +## Examples |
| 72 | + |
| 73 | +### Set custom selector priority |
| 74 | + |
| 75 | +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) (which will be replaced by Cypress Studio once it is no longer experimental). |
| 76 | + |
| 77 | +For example, this config tells Cypress to prefer semantic and accessibility attributes before falling back to styling details like class names. |
| 78 | + |
| 79 | +```javascript |
| 80 | +Cypress.ElementSelector.defaults({ |
| 81 | + selectorPriority: [ |
| 82 | + 'attribute:role', |
| 83 | + 'attribute:aria-label', |
| 84 | + 'name', |
| 85 | + 'class', |
| 86 | + 'attributes', |
| 87 | + ], |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +### Prioritize accessible attributes |
| 92 | + |
| 93 | +Accessibility-first apps often use ARIA roles and labels. You can configure Cypress to prioritize these when generating selectors: |
| 94 | + |
| 95 | +```js |
| 96 | +Cypress.ElementSelector.defaults({ |
| 97 | + selectorPriority: ['attribute:aria-label', 'attribute:role', 'id', 'class'], |
| 98 | +}) |
| 99 | +``` |
| 100 | + |
| 101 | +This helps produce more readable and resilient selectors, especially for accessibility-first applications. |
| 102 | + |
| 103 | +### Prioritize language-agnostic selectors (for i18n) |
| 104 | + |
| 105 | +In multilingual applications, selectors based on text or labels may change between locales. Prefer stable, language-agnostic attributes like `data-*`, `role`, and `aria-labelledby`. |
| 106 | + |
| 107 | +```js |
| 108 | +Cypress.ElementSelector.defaults({ |
| 109 | + selectorPriority: [ |
| 110 | + 'data-cy', |
| 111 | + 'attribute:role', |
| 112 | + 'attribute:aria-labelledby', |
| 113 | + 'name', |
| 114 | + 'id', |
| 115 | + 'class', |
| 116 | + 'attributes', |
| 117 | + ], |
| 118 | +}) |
| 119 | +``` |
| 120 | + |
| 121 | +This ensures selectors are resilient to translation changes in text or labels. |
| 122 | + |
| 123 | +### Avoid dynamic or auto-generated selectors |
| 124 | + |
| 125 | +Many frameworks produce dynamic ids or class names such as: |
| 126 | + |
| 127 | +```html |
| 128 | +<button |
| 129 | + id="button-5a3f9d" |
| 130 | + class="Component_button__3XyZ2 css-1a2b3c SeriesIndexFooter-footer-3WmRg" |
| 131 | + data-cy="checkout-btn" |
| 132 | +> |
| 133 | + Checkout |
| 134 | +</button> |
| 135 | +``` |
| 136 | + |
| 137 | +You can configure Cypress to skip attributes that are dynamically generated. |
| 138 | + |
| 139 | +```js |
| 140 | +Cypress.ElementSelector.defaults({ |
| 141 | + selectorPriority: [ |
| 142 | + 'data-cy', |
| 143 | + 'attribute:role', |
| 144 | + 'attribute:aria-label', |
| 145 | + 'name', |
| 146 | + 'attributes', // fallback |
| 147 | + // deliberately omit 'id' and 'class' |
| 148 | + ], |
| 149 | +}) |
| 150 | +``` |
| 151 | + |
| 152 | +## History |
| 153 | + |
| 154 | +| Version | Changes | |
| 155 | +| ------------------------------------------ | ------------------------------------------------------------------ | |
| 156 | +| [15.0.0](/app/references/changelog#15-0-0) | Renamed `Cypress.SelectorPlayground` to `Cypress.ElementSelector`. | |
| 157 | + |
| 158 | +## See also |
| 159 | + |
| 160 | +- [Cypress Studio](/app/guides/cypress-studio) |
| 161 | +- [Selector Playground](/app/core-concepts/open-mode#Selector-Playground) |
0 commit comments