首页 > 解决方案 > 基于文本循环遍历 cypress 元素

问题描述

我要测试的页面有多个带有文本Order的按钮。我正在尝试获取所有这些按钮并循环它们。

我不能使用cy.get('button).contains('Order'),因为它只会返回第一个按钮。有没有办法做到这一点?

我什至试图循环所有按钮

cy.get('button').each(($button, index) => {
    if ($button.text() == 'Order') {
        cy.wrap($button).click();
    }
});

但这会在第二个按钮上引发错误

cy.click() failed because this element is detached from the DOM.

任何帮助表示赞赏。

标签: typescripttestingcypress

解决方案


推荐阅读