Cypress find command

Profile picture for user arilio666

In cypress, there is a unique way to find web elements using the find() command, Querying behavior of find() command is exactly same as JQuery find() method.

So typically, a find() command returns one or two DOM elements based on the CSS selector or any other parameter.

Now you may be wondering we have get() command for that why we need to see find()?

The only difference in find() and get() is that find() can be chained with other methods and cannot be used directly with the object cyfind() can only be chained with methods sticking with cy object such as get().

Syntax

.find('selector')
.find(selector, options)
  • Selector is used to filter matching descendent DOM element.
  • You can use log, timeout and includeShadowDOM options to change the default behavior of .find().

Example

describe('Automate AutoPract',()=>{
    it('Should load the url and find the clothing section and assert',()=>{
        
        // visit the site
        cy.visit('http://www.autopract.com/#/home/fashion')
        
        // close a popup that occurs immediately after the load.
        cy.get('.close').click()
        
        // navigating to the sites side list section where all the categories are placed.
        cy.get('.bar-style').click()
        cy.get('#sub-menu').should('be.visible')
        cy.get("ul[id='sub-menu']").find('li a').contains(' clothing ').should('be.visible')
        
    })

})

Above command can also be written as:

cy.get("ul[id='sub-menu']").find('li').find('a').contains(' clothing ').should('be.visible')

 However if you will replace get with find here, it will return error "Oops, it looks like you are trying to call a child command before running a parent command"

cy.find("ul[id='sub-menu'] li a").contains(' clothing ').should('be.visible')

but you can get the same result using get

cy.get("ul[id='sub-menu'] li a").contains(' clothing ').should('be.visible')
  • Here We are automating a site called http://www.autopract.com/#/home/fashion
  • We are using the get() to fetch the web element of the entire sub-menu section using CSS selector
  • Then go to ul->li, and inside li, go the 'a' tag which is the subtag or the child of li, and here We have used the find command to fetch the title.
  • And finally checking whether the clothing section is visible with should().
  • .find() will automatically retry until the element(s) exist in the DOM. Also, It will automatically retry until all chained exception pass.

As the find command is always chained, it is often chained with the get() command to be chained for the child element from a parent.

cy.get(parent).find(child)


  • This is the dom of the browser.
  • Here We have fetched the 'a' tag, which is the subtag of li, and li, which is the subtag of ul.
  • Thus, it is clear that the find() command is used to search for the web elements that are nested or have parent-child relation types of features.
  • That way, it'll isolate the child and gives out the result as quickly as possible.