如何通过文本查找元素?

时间:2021-07-27 13:14:56

标签: javascript cypress

如果在网站上有 design now 按钮或找到 h1 标签,仍然转到 else 部分。

HTML 代码

<main>
  <h1>Return dynamic status code</h1>
  <p>"Off-the-shelf products" are not your thing. Then just design your own.</p>
  <a class="btn btn-secondary" href="/steps">Design Now</a>
</main>

我的代码

// By design now button
    cy.get('body').then((body) => {
        if (body.find('.btn-secondary').contains('Design Now').length > 0) {
            cy.log('Return dynamic status code')
        } else {
            cy.log('remove products')
            s2.productlist().find('.close-img').then(($ele) => {
                cy.get($ele).click({ multiple: true, force: true })
            })
        }
    })

    // by h1 text
    cy.get('body').then((body) => {
        if (body.find('h1').text('Return dynamic status code').length > 0) {
            cy.log('Return dynamic status code')
        } else {
            cy.log('remove products')
            s2.productlist().find('.close-img').then(($ele) => {
                cy.get($ele).click({ multiple: true, force: true })
            })
        }
    })

0 个答案:

没有答案
相关问题