user2451016
user2451016

Reputation: 1961

How to locate element based on text()/contains criteria in Cypress.io

I need to locate an element/ verify presense of an element, based on the Text inside element, using something like:

//div[contains(text(),'<My intended text>')]

As Cypress.io do not support the XPATh yet, then how to locate it using alternate locators. There seems to be lot of discussions around cssselectors :contains & when tried div:contains('<My intended text>') it failed to locate the element. Given the fact that both XPATH & CSSLocators have good performance on modern browsers.

Kindly advise. Thanks

Upvotes: 0

Views: 2500

Answers (1)

Mr. J.
Mr. J.

Reputation: 3741

so you are looking for the parent which holds 'My intended text'?

You can do so by using:

cy.contains('My intended text')

and that's how you selected the element. Now you can use for example .click() to click on the element which holds 'My intended text'

Upvotes: 1

Related Questions