Search code examples
automationcypress

Cypress click element by ID / XPATH / Name?


I want to click on an element by XPATH / ID and not the default cypress locator, is it possible?

In selenium I can use find element by XPATH for example:

d.findElement(By.id("category")).click();

In Cypress it's:

cy.get('#hdtb-msb-vis > :nth-child(3) > .category').click()

Can I click by ID? (It looks better in selenium!)

d.findElement(By.id("category")).click();

VS

cy.get('#hdtb-msb-vis > :nth-child(3) > .category').click()

Solution

  • In Cypress, it works like this:

    cy.get('button[id="category"]').click()
    

    Notice that I just used button as an example here, you should replace that with the label of your element: div, select, textarea, etc...