Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

Locating Elements Containing Text

Category: Cypress

Get Element By Containing Text in Cypress
Cypress provides contains() function along with the tag name and should be able to get the element.
The above approach is most useful to find the href link by Text in Cypress
For Example:
cy.get(‘a:contains(“Forgotten password?”)’).first().click()

Get DOM Element containing Text in Cypress
Cypress provides contains() function, which can be used with cy command or can be chained with cy.get() command to get the DOM element as seen in the examples below
Example 1:
Find the element having Text Sign In
cy.contains(‘Sign In’);

The above command searches for Text ‘Sign In’ inside the web page and returns the element. However, the command doesn’t check which type of element it is.

Example 2:
Find the DOM element having Text Sign In
cy.get(‘a’).contains(‘Sign In’);

The above command searches for all tags and returns the element if is having text ‘Sign In’. So, considering the first example, this approach is more specific to the tag name.

Leave a Reply

Your email address will not be published. Required fields are marked *