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()
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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With