How can I use Cypress to click an element using get by ID?

How can I use Cypress to click an element using get by ID?

Hi Shreshthaseth,

I hope this email finds you well. Thank you for reaching out with your question regarding the usage of cy.get() with an ID selector in Cypress testing. I appreciate the opportunity to provide you with a detailed answer to your inquiry.

Using cy.get() with ID Selector:


Cypress provides the cy.get() command to select elements based on various locators, including ID. You can simply pass the ID selector prefixed with # to cy.get() to locate the element and then chain the .click() command to simulate a click action.

Hi Shreshthaseth

I hope this message finds you well. Regarding your inquiry about using data-testid with cy.get() in Cypress testing, here is a concise explanation:

In situations where your HTML element lacks a unique ID but possesses a data-testid attribute, Cypress allows you to select and interact with it using the cy.get() command. The syntax for this operation is as follows:

Using data-testid with cy.get():


If your element doesn’t have a unique ID but has a data-testid attribute, you can still select it using cy.get() by specifying the attribute and its value. This approach is helpful when the ID is not available or dynamic.