Steerpike
Steerpike

Reputation: 1863

Refining selectors in Cypress

I'm trying to learn best practice for selectors using Cypress.

I have an element buried within many tables within a frame. This is an app I have no control over so cannot add custom selectors for Cypress. The full Xpath is as follows:

/html/body/table/tbody/tr[1]/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td/table/tbody/tr/td/a

Within the a tag is the link I wish to do a click() on.

<a class="menubuttontext" style="cursor:pointer" href="javascript:menuClick(1)">Men's Apparel</a>

Can someone provide the 'best' (or at least relatively resilient) selector? I don't wish to fall back to adding Xpath libraries - I am switching from many years working with Selenium so learning as I go. I'm unclear as to how many attributes I can chain together or even how - I am digging through the documentation but some help would be much appreciated.

Upvotes: 0

Views: 131

Answers (1)

PeaceAndQuiet
PeaceAndQuiet

Reputation: 1800

If you can rely on the text content of your link element, you may try with Cypress command cy.contains():

cy.contains('a.menubuttontext', "Men's Apparel").click();

You can also use a regex for the 2nd argument to fine tune your search.

Upvotes: 0

Related Questions