【问题标题】:How can I find and click button element in cypress by the text on the button如何通过按钮上的文本找到并单击 cypress 中的按钮元素
【发布时间】:2022-07-30 01:46:03
【问题描述】:

我很难找到用柏树定位此元素的方法。由于它的复杂性,我不想使用类名,并且想使用“支持”来保持我的代码干净。

<button type="button" class="border-secondary shadow-none btn btn-outline-primary">Support</button>

我尝试 cy.get('button').contains('Support').click() 没有成功。

【问题讨论】:

    标签: button cypress


    【解决方案1】:

    .contains() 的另一种变体只为您提供“支持”按钮

    cy.contains('button', 'Support')
    

    【讨论】:

      【解决方案2】:

      使用 cy.contains 的唯一问题是,如果您想验证其上的 css 颜色或文本怎么办? cy.contains 大部分时间都不起作用。 我个人仍然会使用该类,但添加通配符“*” 前任: cy.get('button[class*="btn-outline-primary"]') button[class*="btn-outline-primary"]

      cy.get('button[class*="btn-outline-primary"]').should(verifyText =>
      {
           expect(verifyText).have.text('Support').have.css('background-color', 'rgb(0, 0, 0)')
      

      })

      当你只想做一些像点击这样的小事时,尝试使用 cy.contains()。但是,如果您需要断言,请尝试使用 cy.get() 对其进行更多定义。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-06-30
        • 1970-01-01
        • 1970-01-01
        • 2021-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多