如何选择柏树中不包含特定值或一组值的所有元素



我有一个列表和值:

selector: ".list > div"
const velue = [6, 9, 21]

每个div包含不同的值。我想删除包含特定值的每个元素,然后选择for instance first element并单击它。像这样:

cy.get(".list > div").not('contains', value).eq(0).click()

我试着用not来处理这种情况,但是它不起作用,只接受选择器。任何想法?我甚至在stackoverflow上都找不到类似的话题。

Cypress附带了整洁的实用程序lodash函数,其中一个是_.filter()。可以通过回调函数过滤出6921

HTML例子

<ul class="list">
<div>1</div>
<div>2</div>
<div>6</div>
<div>3</div>
<div>9</div>
<div>5</div>
<div>21</div>
</ul>

测试代码

cy.get(".list > div")
.then(($el) => {
// filter out elements having '6', '9', and '21'
// and return the array to cypress chain
return Cypress._.filter($el, (el) => {
const text = el.innerText;
return text != "6" && text != "9" && text != "21";
});
})

下面是工作示例。

可以使用:not选择器,看起来是这样的如果你想要排除"3">

等"5";
cy.get('div:not(:contains("3"), :contains("5"))')

value需要合并到.not()命令中的contains选择器中。

它实际上是一个伪选择器,所以格式是:contains()

cy.get(".list > div")
.not(`:contains(${value})`)   // inject value into string template
.eq(0)
.click()

最新更新