Skip to content
Advertisement

How to get value of a child based on the text of another child within the same JavaScript parent

so I want to be able to get “Text B” from the table below using Cypress but have tried many things and I can only get “Party B”.

JavaScript

I have been using cy.get(div[class="table"]).contains(div[class="label"], “Party B”).click() to click on the text for “Party B”, but don’t know how to click on the other child of the same parent.

Advertisement

Answer

To get the table containing “Party B”, specify the selector inside .contains()

JavaScript

There are variations you could use, if you know “Text B” is the actual text

JavaScript

If you want to navigate first to div[class="table"], then to div[class="label"]

JavaScript

Be careful about doing multiple clicks in a chain, usually they have side-effects.

It’s better to start a new chain after each click.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement