Cypress: Locating Button by Specific Text

What is the Cypress command to find a button with specific text?

I’ve had quite a bit of experience with Cypress! If you’re looking to find a button with specific text, one handy method is to utilize contains(). This function helps you locate elements containing the text you specify. For instance:


cy.contains('button', 'Submit');

Building upon that, another effective approach is to combine contains() with a CSS selector. This allows for more targeted selection, especially if you’re dealing with multiple buttons.


cy.get('button').contains('Submit');

Absolutely, and if you’re dealing with a button that possesses a unique class or ID, utilizing that along with contains() can be even more precise in locating the desired button.


cy.get('.submit-button').contains('Submit');