[英]How to select all elements that do not contain a particular value or set of values in cypress
I have list and values:我有列表和值:
selector: ".list > div"
const velue = [6, 9, 21]
each div contain different value.每个 div 包含不同的值。 I want to rid of each element that contain a particular value, then select for instance first element and click on it.
我想删除包含特定值的每个元素,然后选择例如第一个元素并单击它。 Something like this:
像这样:
cy.get(".list > div").not('contains', value).eq(0).click()
I tried to handle this case using not but it doesnt work in that way, only accept selectors.我尝试使用not来处理这种情况,但它不会以这种方式工作,只接受选择器。 Any idea?
任何的想法? I cant even find similar topic on stackoverflow.
我什至无法在 stackoverflow 上找到类似的主题。
Cypress comes bundled with neat utility lodash functions, one of which is _.filter()
. Cypress 捆绑了实用的 lodash 函数,其中之一是
_.filter()
。 You can use a callback function to filter out the 6
, 9
, and 21
.您可以使用回调函数过滤掉
6
、 9
和21
。
example HTML示例 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>
Test code测试代码
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";
});
})
You can use the :not selector, so it look like this if you want to exclude the "3" et "5"你可以使用:not选择器,所以如果你想排除“3”和“5”,它看起来像这样
cy.get('div:not(:contains("3"), :contains("5"))')
The value
needs to be merged into the contains
selector inside the .not()
command.该
value
需要合并到.not()
命令内的contains
选择器中。
It's actually a pseudo-selector , so format is :contains()
它实际上是一个伪选择器,所以格式是
:contains()
cy.get(".list > div")
.not(`:contains(${value})`) // inject value into string template
.eq(0)
.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.