1

I'm trying to check if an element exists or not. If the element exists, I want to click on the button X, I want to click on the button Y.

However, when the element does not exist Cypress throws an exception and it stops the test.

enter image description here

Code Sample:

let elementKey = '#up-picker'
Cypress.get('body').then($res=>{
  if($res.find(elementKey).length>0){
   // click on button X
  }else{
    // click on button Y
  }
})

I have an element with this id, but sometimes because the logic in the UI changes it, Cypress can find this element.

I checked more posts in Stackoverflow and no answer works for me.

1
  • @Mansprof would increasing the timeout help you with your case ? Commented Aug 24, 2020 at 6:59

1 Answer 1

1

What's working for my tests (close to your example):

cy.get('body').then($body => {
  if ($body.find('your-selector').length) {
    // do stuff
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.