Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 like:

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()
like image 533
Spartacus Avatar asked Jul 04 '19 11:07

Spartacus


People also ask

How do you use the ID selector in Cypress?

Get HTML Element by ID Selector in Cypress ID is an attribute of an HTML tag, which is used to find an HTML element. Using the Cypress command – cy. get(), you can directly pass the id with prefix # and get the element.

How use xpath selector in Cypress?

To use xpath selectors, you must first install a plugin. It is an official plugin maintained by Cypress. The installation is pretty standard. Just npm install -D cypress-xpath to install the package.

Does Cypress support xpath selectors?

Note: Cypress does support Xpath selectors as well. However, that does not come by default. In other words, we need 'Cypress-Xpath' external plugins to assist this selector.


1 Answers

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...

like image 87
GonzaSSH Avatar answered Sep 18 '22 11:09

GonzaSSH