Cypress click element by ID / XPATH / Name?

前端 未结 5 1891
一整个雨季
一整个雨季 2021-02-20 11:40

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:



        
5条回答
  •  伪装坚强ぢ
    2021-02-20 11:55

    I think, it is possible by adding a plug-in as suggested in Cypress website, please refer the following link https://docs.cypress.io/plugins/#content. If you refer the custom command section you could see cypress-xpath which takes you to following github link https://github.com/cypress-io/cypress-xpath

    npm install -D cypress-xpath
    

    Then include in your project's cypress/support/index.js

    require('cypress-xpath')
    

    Sample usage given below:

    it('finds list items', () => {
      cy.xpath('//ul[@class="todo-list"]//li')
        .should('have.length', 3)
    })
    

    Please try after installing the plug-in and updating the support/index.js file.

提交回复
热议问题