Search code examples
javascriptautomationcypress

click on href without id or class cypress


I'm trying to click an href element using cypress I've wondered if I can access it by its content ex:

<a href=example.com>CONTENT</a>

but I couldn't find anything online.

I've tried using something like that:

cy.get('a[href*="url"]').click()

but it also didn't work


Solution

  • You can use contains to select elements by textual content, like this:

    cy.get('a').contains('CONTENT').click();
    

    Note that the used href attribute value is invalid, protocol and quotes are missing, should be something like this:

    <a href="https://example.com/">CONTENT</a>

    Documentation can be found here: https://docs.cypress.io/api/commands/contains