Chaining selectors helps you find elements inside other elements easily. It makes tests clear and simple.
Chaining selectors in Cypress
cy.get('parentSelector').find('childSelector')
cy.get() finds the parent element first.
.find() looks inside the parent for the child element.
cy.get('form#login').find('input[type="text"]')
cy.get('ul.menu').find('li.active')
cy.get('nav').find('a').should('have.length', 5)
cy.get('section.content').find('p').first().should('contain', 'Welcome')
This test visits a page, finds the search form, then finds and clicks the submit button inside it. Finally, it checks the URL changed to include 'search'.
describe('Chaining selectors example', () => { it('Finds and clicks a button inside a form', () => { cy.visit('https://example.cypress.io') cy.get('form#search').find('button[type="submit"]').click() cy.url().should('include', 'search') }) })
Chaining selectors improves test accuracy by narrowing down elements.
It reduces flakiness by avoiding selecting wrong elements with similar classes or tags.
Use chaining instead of long complex selectors for better readability.
Chaining selectors means finding a parent element first, then searching inside it.
This helps target elements precisely and write clearer tests.
Use cy.get() followed by .find() to chain selectors in Cypress.
Practice
cy.get() followed by .find() means:Solution
Step 1: Understand
cy.get()usagecy.get()selects a parent or root element on the page.Step 2: Understand
.find()usage.find()searches inside the previously selected element for child elements.Final Answer:
Find a parent element first, then search inside it -> Option CQuick Check:
Chaining selectors = parent then child [OK]
- Thinking .find() selects elements globally
- Confusing .find() with .get()
- Assuming chaining skips elements
- Believing chaining selects randomly
Solution
Step 1: Identify correct order of chaining
First select the parent withcy.get('#login').Step 2: Use
Then use.find()to locate child button.find('button')to get the button inside the form.Final Answer:
cy.get('#login').find('button') -> Option AQuick Check:
Parent get, then child find = A [OK]
- Swapping get() and find() order
- Using find() as first method
- Selecting child before parent
- Incorrect selector syntax
cy.get('.menu').find('li.active')Solution
Step 1: Understand
This selects all elements with class 'menu'.cy.get('.menu')Step 2: Understand
This finds all.find('li.active')lielements with class 'active' inside those 'menu' elements.Final Answer:
Alllielements with class 'active' inside elements with class 'menu' -> Option DQuick Check:
get('.menu') then find('li.active') = C [OK]
- Thinking find() searches globally
- Confusing combined classes with nested elements
- Assuming only first element is selected
- Ignoring the chaining order
cy.find('.container').get('button.submit')Solution
Step 1: Check method order
cy.find()is not a valid Cypress command;cy.get()should be used first to select elements.Step 2: Confirm correct chaining
The correct order iscy.get('.container').find('button.submit').Final Answer:
Usingfindbeforegetis incorrect -> Option AQuick Check:
get() first, then find() [OK]
- Using cy.find() instead of cy.get()
- Swapping get() and find() order
- Assuming find() works as a root selector
- Ignoring Cypress command syntax
li has a button inside. Which Cypress code correctly clicks the button inside the third li element within a ul with class 'items'?Solution
Step 1: Select the parent list
cy.get('ul.items')selects theulwith class 'items'.Step 2: Find all
lichildren and pick the third.find('li').eq(2)selects the thirdli(index starts at 0).Step 3: Find the button inside that
liand click.find('button').click()clicks the button inside the selectedli.Final Answer:
cy.get('ul.items').find('li').eq(2).find('button').click() -> Option BQuick Check:
Parent get, child find, eq(2), then find button [OK]
- Using eq(3) instead of eq(2) (index off by one)
- Selecting button before li
- Using eq on wrong element
- Not chaining find() properly
Ready to prove it?
Pass the test and this pattern moves unknown
