Not able to click a link which is part of business details container listed on home page using JavaScript in cypress

Viewed 55

There are multiple businesses listed on a home page and some business names may have the same name. I have to click on the link 'Apply for Business Credit' and it would navigate to a different screen. I have tried multiple solutions but not able to click on desired link. Please have a look on the home screen enter image description here

I have also added html for your reference. enter image description here

Solutions I have tried -

    cy.get('div.MuiGrid-root.MuiGrid-item').contains('Shemaroo Equities').eq(0)
    .nextUntil('div')
    .contains('p','Apply for Business Credit')
    .click({force:true})

    cy.get('div.MuiGrid-root.MuiGrid-item').contains('Shemaroo Equities').eq(0).nextUntil('div button span.MuiTouchRipple-root').click()

I am relatively new to cypress. I request the community to help me resolve this issue. enter image description here

2 Answers

//Try below code to click on the first instance of "Shemaroo Equities"

 cy.contains('ShemarooEquities')
.eq(0).parent().siblings().eq(1).within(()=>{
cy.contains('Apply for Business Credit').click()
})

or

cy.contains('ShemarooEquities')
    .eq(0).parent().next().next().within(()=>{
    cy.contains('Apply for Business Credit').click()
    })

or

cy.contains('ShemarooEquities')
    .eq(0).parent().parent().within(()=>{
    cy.contains('Apply for Business Credit').click()
    })

It should be quite simple to get the first instance of a business card by using cy.contains('selector', 'text') with an added chained .contains() to get the 'Apply for Business Credit' link.

// yields the first card with Shemaroo Equities
cy.contains('.MuiGrid-container', 'Shemaroo Equities')
  .should('be.visible')
  // chained queries are limited to previous subject
  .contains('Apply for Business Credit')
  .should('be.visible')
  .click()
Related