skip to Main Content
//1st Selector
cy.get('div.d-flex.justify-content-between.undefined > button.button.button-filled').contains('Yes, delete user');

//2nd selector
cy.get('div.modal.fade.show > div > div > div > div:nth-child(3) > button.button.button-filled').contains('Delete User');

So i got 2 selector, can you help me to make if/else condition, if the 1st condition not working then run the 2nd selector?

thank you!

2

Answers


  1. You can compose selectors with a comma between to perform either/or logic.

    For example (just using button caption because it seems unique)

    const selector1 = ':contains("Yes, delete user")'
    
    const selector = ':contains("Delete User")'
    
    cy.get(`${selector1}, ${selector2}`)
    

    If you want you can add all those other selectors, but it looks a bit over-done to me.

    Login or Signup to reply.
  2. If you page has finished loading, you can use the following code to click one button or the other

    cy.get('body').then(body => {
      const button1 = body.find('div.d-flex.justify-content-between.undefined > button.button.button-filled:contains("Yes, delete user")')
      const button2 = body.find('div.modal.fade.show > div > div > div > div:nth-child(3) > button.button.button-filled:contains("Delete User")')
      if (button1.length) {
        button1.click()
      } else if if (button1.length) {
        button2.click()
      } else {
        throw new Error('no button to click')
      }
    })
      
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search