Uploaded June 2023 | Updated September 2026, 1 week ago
You can use the versatile cy.contains command to find elements on the page by text. You can also:
- ignore the text case using "matchCase: false" option
- use regular expressions
- construct a regular expression if the string to find comes from a variable
For example:
const value = 'hello'
const regex = new RegExp('^' + value, 'i')
cy.contains('#greeting', regex)
Find this and many more examples at glebbahmutov.com/cypress-examples
You can use the versatile cy.contains command to find elements on the page by text. You can also:
- ignore the text case using "matchCase: false" option
- use regular expressions
- construct a regular expression if the string to find comes from a variable
For example:
const value = 'hello'
const regex = new RegExp('^' + value, 'i')
cy.contains('#greeting', regex)
Find this and many more examples at glebbahmutov.com/cypress-examples






![Dispatch Custom HTML Events From Cypress Tests
Sometimes an element on the page subscribes to custom events. You can dispatch events using plain HTML method el.dispatchEvent or by using Cypress command cy.trigger
cy.get(#signal).then(function ($el) {
$el[0].dispatchEvent(new CustomEvent(go))
})
// using cy.trigger
cy.get(#signal).trigger(warn)
Find the full example recipe at https://glebbahmutov.com/cypress-examples/ Dispatch Custom HTML Events From Cypress Tests](https://i.ytimg.com/vi/qCqzS3LtSeI/mqdefault.jpg)



