CSS Selectors in Cypress

On This Page What are CSS picker?1. Basic CSS SelectorsJune 04, 2026 · 6 min read · Tool Comparison

CSS Selectors in Cypress

More often it is required to test the demeanour of certain elements on the web coating. Selectors help to identify those elements while writing the tests. In Cypress, CSS selectors are the only locators usable. However, it can likewise act with XPath, with the assistant of an additional plugin.

Overview

Types of CSS Selectors

  • Basic selectors: ID (# id), family (.class), type/tag selectors (div, yoke)
  • Attribute selectors: [attr], [attr= & # 8221; value & # 8221;], starts-with ^=, ends-with $ =, contains * =
  • Combination selectors: descendent, minor (& gt;), sibling (+, ~), grouping (,), multiple class & amp; type combination

How to Use CSS Selectors in Cypress

  • Setup Cypress project (npm init, instal Cypress)
  • Configure Cypress and write test specification and run the specification file.

Checking CSS Styles via Tests

  • Use .invoke (& # 8220; val & # 8221;, & # 8230;) and .trigger (& # 8220; change & # 8221;) to assume UI actions
  • Use .should (& # 8220; have.css & # 8221;, & # 8220; property & # 8221;, & # 8220; value & # 8221;) to asseverate fashion changes (e.g. background color)

This guide will discuss different CSS selectors and how to use them with the Cypress framework.

What are CSS selectors?

CSS selectors are the pattern used to identify elements on a web page ground on their style.

Also Read:

There are a number of selectors available in CSS given as below:

1. Basic CSS Selectors

Selector NameDescriptionExample in Cypress
ID SelectorCSS ID Selector is an attribute that is passed to an HTML element. One can use the#symbol along with the value of the ID attribute to get the factor.
cy.get (‘ # login-btn ’)

Matches the ingredient that has the same id as ‘ login-btn ’.

Class SelectorClass is another attribute of an HTML component that can be used to name the constituent utilise dot (.) with the value of the class attribute.
cy.get (‘ .email ’)

Matches the elements that have the like family as ‘ e-mail ’.

Type SelectorOne can also use the tag gens or type of the HTML element to select it from the page.
cy.get (‘ span ’)

Matches the ‘ span ’ constituent from the given page.

2. CSS Attribute Selectors

Selector NameDescriptionExample in Cypress
Has Attribute SelectorThe HAS chooser just checks to see if an factor has the specified attribute.
cy.get (‘ [data-active] ’)

Matches the element that has the attribute ‘ data-active ’ set.

Exact Attribute SelectorThis chooser will only match elements that feature the specified dimension with the precise specified value.
cy.get (‘ [data-active= ” true ”] ’)

Matches the element that has the attribute ‘ data-active ’ set to true.

Begins with SelectorIf one desire to ensure for an dimension that commence with a specific value so thebegins withdimension selector is helpful.
cy.get (‘ [data-color^= ” r ”] ’)

Matches the component that has the attribute ‘ data-color ’ that begins with the letter ‘ R ’.

Ends with SelectorThis chooser is indistinguishable to theBegin withattribute selector but checks the end of the value alternatively.
cy.get (‘ [data-color $ = ” r ”] ’)

Matches the ingredient that has the attribute ‘ data-color ’ that ends with the letter ‘ r ’.

Substring Attribute SelectorThis is like to the last two picker, but it checks that the string passed to it appear anyplace in the property value.
cy.get (‘ [data-color * = ” e ”] ’)

Matches the element that has the attribute ‘ data-color ’ in which the letter ‘ e ’ appears.

3. Combination Selectors

Selector NameDescriptionExample in Cypress
Descendant SelectorThis picker permit one to select any element that matches a specific selector which is a descendant of an element that matches a different picker.
cy.get (‘ .container p ’)

Matches all the p tag that get under the ingredient with the class gens ‘ container ’.

CSS Child SelectorCSS kid selector allows you to take only the direct child factor of an HTML ingredient.
cy.get (‘ .container & gt; p ’)

Matches all the p tags that come directly under the element with the family name ‘ container ’.

General Sibling SelectorThis selector grant you to select sibling ingredient that come after an HTML element.
cy.get (‘ .box ~ p ’)

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

Matches all the p tag that come after the element with the course gens box.

Conterminous Sibling SelectorIt is similar to the general sibling selector but can just take the siblings that come directly after the element.
cy.get (‘ .box + p ’)

Matches the p tag that come directly after the element with the category name box.

Or SelectorThis selector grant you to select elements that match at least one of the selectors.
cy.get (‘ .square, # rect ’)

Matches the factor that have either class name ‘ square ’ or id attribute equal to ‘ rect ’.

And SelectorThis chooser allows one to choose elements that match all the selectors condition.
cy.get (‘ div.black ’)

Matches the div that has a class name adequate to black.

Note:Here is the complete list of all theCSS Selectors available

How to use CSS Selectors in Cypress?

Step 1 Setup project with Cypress

To set up a new project in Cypress follow the below steps

  • Initialize a new project
npm init -y
  • Install Cypress
npm i cypress
  • Verify Cypress installation
npx cypress verify
  • Run Cypress
npx cypress exposed

Step 2 Configure Cypress

  • A new window like below will be opened, Click‘ E2E Testing ’to configure it.
  • New files related to Cypress will be configured for the project. Click‘ Continue ’.
  • Select a browser to run tests.

Also Read:

  •  Click ‘ Create new empty spec ’
  • Choose a path for the spec file and click‘ Create Spec ’
  • Run the specification file by chatter on‘ Okay, run the spec ’
  • You shall see the default test being passed on the browser.

Let & # 8217; s publish a examination to see if a user can log in tohttps: //bstackdemo.com/

describe (`` CSS selectors in Cypress Tutorial '', () = & gt; {it (`` Login with Demo User '', () = & gt; {cy.visit (`` https: //bstackdemo.com/signin ''); cy.get (`` # react-select-2-input '') .type (`` demouser '', {force: true}) .type (`` {enter} ''); cy.get (`` # react-select-3-input '') .type (`` testingisfun99 '', {force: true}) .type (`` {enter} ''); cy.get (' # login-btn ') .click ();});});

Save the file. Cypress will mechanically run the test.

How to Check CSS style using Cypress

Let ’ s write another test in which we quiz the coating that vary a CSS variable using Cypress Check CSS Style.

Also Read:

The demo application simply contains an input element of type= ” color ” as shown below.

When one choose a color, the background color of the web page alteration with that color.

Let & # 8217; s write a test to ascertain the same using Cypress Check CSS style.

describe (`` Check CSS style '', () = & gt; {it (`` changes background color '', () = & gt; {cy.visit (`` http: //127.0.0.1:5500/index.html ''); cy.get (`` # color-picker '') .invoke (`` val '', `` # 33BEFF '') .trigger (`` change ''); cy.get (`` body '') .should (`` have.css '', `` background-color '', `` rgb (51, 190, 255) '');});});

In the above snippet, we visit the URL where the application is running and select the input factor using its ID selector. Then the invoke() method passes the value of test color & # 8220; # 33BEFF & # 8221; to the input element and thetrigger()method actuate the alteration event.

The should()method checks if the ground color of the body element is actually changed on the page.

Run the test and see the result.

It is highly important to perform these tests on existent devices. BrowserStack allow you to fulfil your Cypress test files on multiple platforms and multiple browser.

The cloud platform of BrowserStack allow the testers to do these tests under real-world conditions on multiple devices available at the same time.

Tags
29,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free