I am facing an issue where I need to interact with a button that appears multiple times on the website within nested cards
.
Specifically, I am trying to locate the card
containing a pet named Bala
, as shown in the attachment below, and click on the Detail
labeled button within that card.
So far, I have managed to identify the correct card
but have only been able to click on the first instance of the Detail
button.
HTML:
<div _ngcontent-eng-1="" class="col-md-3 pt-3 pb-3">
<app-pet _ngcontent-eng-1="" _nghost-eng-2="" class="dog"><div _ngcontent-eng-2="" class="pet">
<!--template bindings={}--><div _ngcontent-eng-2="" class="card">
<div _ngcontent-eng-2="" class="card-block">
<h4 _ngcontent-eng-2="" class="card-title">Bala</h4> //Checking this?
<h6 _ngcontent-eng-2="" class="card-subtitle mb-2">
<!--template bindings={}--><div _ngcontent-eng-2="" class="badge badge-pill badge-success">
New
</div>
<!--template bindings={}--><div _ngcontent-eng-2="" class="badge badge-pill badge-info">
Important
</div>
</h6>
<ul _ngcontent-eng-2="" class="list-group list-group-flush">
<li _ngcontent-eng-2="" class="list-group-item">
<div _ngcontent-eng-2="" class="age">
<span _ngcontent-eng-2="">Age: </span>
<span _ngcontent-eng-2="">20</span>
</div>
</li>
<li _ngcontent-eng-2="" class="list-group-item">
<div _ngcontent-eng-2="" class="type">
<a _ngcontent-eng-2="" href="/pet/494">
<img _ngcontent-eng-2="" src="/assets/dog.gif">
</a>
</div>
</li>
<li _ngcontent-eng-2="" class="list-group-item">
<a _ngcontent-eng-2="" class="btn btn-primary" href="/pet/494">
Detail
</a> //CLICK
</li>
</ul>
</div>
</div>
</div>
</app-pet>
</div>
My code
Then('I click on the Detail button', () => {
cy.get('div.card').contains('Bala').find('a.btn.btn-primary').click();
});
This is my current approach, however, I have experimented with different methods such as using find('a').contains('Detail')
or
find('a').contains('btn.btn-primary')
, among others.
Graphically
Here is a visual representation of the site
I have also explored similar queries on SO, but none seemed to provide a suitable solution for my specific scenario.