简体   繁体   English

如何在赛普拉斯中选择不包含特定值或一组值的所有元素

[英]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 .您可以使用回调函数过滤掉6921

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";
    });
  })

Here is working example .这是工作示例

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.

相关问题 如何使用jQuery选择具有特定ARIA值的所有元素? - How to select all elements with particular ARIA value using jQuery? 如何使用 cypress/javascript 从 select 中调用所有选项值? - How to invoke all option values from select with cypress/javascript? 获取包含特定后代的所有元素 - Get all elements that contain a particular descendant 如何从选择器中获取所有元素包含的属性的每个值? - How do you get every value for an attribute that all the elements contain from a selector? 使用jQuery将所有选择框值设置为特定值 - Setting all select box values to particular value using jQuery jQuery-如何选择具有相同类但不具有特定ID的所有元素 - jQuery - How to select all elements with same Class but not with particular ID 如何检查 HTMLCollection 中的所有元素是否都包含某个属性的特定值? - How to check if all elements in an HTMLCollection contain a certain value for a property? 如何选择具有 .innerHTML 属性的元素的所有值 - how to select all values of elements with a .innnerHTML property 如何在 Jquery 或 Cypress 中选择和显示具有相同类的所有项目和元素? - How to select and show all items and the elements inside with the same class in Jquery or Cypress? 如何选择页面上包含特定单词的所有链接? - How do I select all links on page that contain a certain word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM