We use cy.get() to find elements on a web page so we can test them. CSS selectors help us pick exactly the right elements.
cy.get() with CSS selectors in Cypress
cy.get('css-selector')
The css-selector is a string that tells Cypress which element(s) to find.
You can use any valid CSS selector like class, id, attribute, or element type.
submit-button.cy.get('#submit-button')
nav-item.cy.get('.nav-item')
name equal to email.cy.get('input[name="email"]')
button elements with the class primary.cy.get('button.primary')
This test visits a login page, finds the email and password fields by their CSS selectors, types in values, clicks the submit button, and checks if a welcome message appears.
describe('Login form test', () => { it('should find and type email', () => { cy.visit('https://example.com/login') cy.get('input#email').type('user@example.com') cy.get('input#password').type('password123') cy.get('button[type="submit"]').click() cy.get('.welcome-message').should('be.visible') }) })
Always use unique and stable CSS selectors to avoid flaky tests.
Use IDs when possible because they are fast and unique.
You can chain cy.get() with other commands like .click(), .type(), and .should().
cy.get() finds elements using CSS selectors.
Use simple and clear CSS selectors for reliable tests.
Combine cy.get() with actions and assertions to test web pages.
Practice
cy.get() do in Cypress testing?Solution
Step 1: Understand the purpose of
cy.get()cy.get()is used to locate elements on the web page by using CSS selectors.Step 2: Differentiate from other Cypress commands
Other commands like clicking or running code are separate;cy.get()only finds elements.Final Answer:
Finds elements on the page using CSS selectors -> Option DQuick Check:
cy.get() = Finds elements [OK]
- Thinking cy.get() clicks elements
- Confusing cy.get() with running scripts
- Assuming cy.get() starts tests
btn-primary using cy.get()?Solution
Step 1: Understand CSS selector for class
In CSS, a class selector uses a dot before the class name, so.btn-primaryselects elements with that class.Step 2: Check each option
cy.get('#btn-primary') uses an ID selector (#), which is wrong here. cy.get('btn-primary') misses the dot, so it looks for a tag named 'btn-primary'. cy.get('button .btn-primary') incorrectly uses a descendant selector (space after button), selecting buttons containing .btn-primary elements.Final Answer:
cy.get('.btn-primary') -> Option CQuick Check:
Class selector = .classname [OK]
- Using # instead of . for classes
- Omitting the dot before class name
- Confusing tag selectors with class selectors
<ul> <li class='item'>Apple</li> <li class='item'>Banana</li> <li class='item'>Cherry</li> </ul>
What will
cy.get('.item').should('have.length', 3) check?Solution
Step 1: Understand
This selects all elements with the class 'item', which are the three <li> elements.cy.get('.item')Step 2: Understand
This asserts that the number of selected elements is exactly 3..should('have.length', 3)Final Answer:
That there are exactly 3 elements with class 'item' -> Option AQuick Check:
cy.get('.item').length = 3 [OK]
- Thinking it checks content inside elements
- Confusing child count with class count
- Assuming it checks number of lists
cy.get('button#submit').click() but the test fails with "Cannot find element" error. What is the most likely cause?Solution
Step 1: Analyze the selector
This selector looks for a <button> element with ID 'submit'. If the button does not have this ID, it won't be found.button#submitStep 2: Check other options
The selector should becy.get('#submit button')reverses the selector incorrectly. You must usecy.find()instead ofcy.get()is wrong becausecy.get()is correct for global selection. The click() command is not supported on buttons is false;click()works on buttons.Final Answer:
The button does not have an ID 'submit' -> Option AQuick Check:
Selector must match element attributes [OK]
- Using wrong selector syntax
- Assuming click() doesn't work on buttons
- Confusing cy.get() with cy.find()
<li> element inside a <ul id='menu'>. Which cy.get() selector is correct?Solution
Step 1: Understand CSS nth-child selector
li:nth-child(2)selects the second child <li> element inside its parent.Step 2: Evaluate each option
cy.get('ul#menu li[2]') uses invalid attribute selector. cy.get('#menu li:eq(2)') uses jQuery syntax not supported in CSS selectors. cy.get('ul#menu > li:nth-type(2)') uses invalid 'nth-type(2)' selector.Final Answer:
cy.get('#menu li:nth-child(2)') -> Option BQuick Check:
Use nth-child(n) to select nth element [OK]
- Using jQuery :eq() in CSS selectors
- Using attribute selectors incorrectly
- Confusing nth-child with nth-of-type
Ready to prove it?
Pass the test and this pattern moves unknown
