I am using Cypress.io to test my startup's website and would like to select a specific anchor tag.
However, that anchor tag does not have any unique attribute I can use to identify it, therefore I would like to use a child h1
tag containing the text "Visit Site" to help with identifying the anchor tag.
<a href="/blablabla" target="_blank" rel="noopener noreferrer canonical">
<h1>Visit Site</h1>
</a>
I would not be able to use the href
attribute to help identify the anchor tag, as the written test would be used across other pages of the same structure but of different content.
My issue is that I do not know if its possible to select an element based on whether it contains an element of a certain text.
cy.contains('a > h1', 'Visit Site').parent()
. Thecy.contains(...)
yields theh1
and.parent()
shifts focus back toa
and yields it to the next command chained. – Richard Matsen