首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >柏树是否支持多重检查条件?

柏树是否支持多重检查条件?
EN

Stack Overflow用户
提问于 2021-10-13 11:22:13
回答 3查看 678关注 0票数 1

我在柏树中有以下代码:

代码语言:javascript
运行
复制
    for(let i = 0; i < lengthOfRow; i++){
        if (
            Cypress.$(':nth-child(1) > :nth-child(6) > .index_textBlock_20DCh').eq(0).text().length == 0 &&
            Cypress.$(':nth-child(1) > :nth-child(7) > .index_textBlock_20DCh').eq(1).text().length == 0 &&
            Cypress.$(':nth-child(1) > :nth-child(8) > .index_textBlock_20DCh').eq(2).text().length == 0
          ) {
            //Do Something
            cy.get('tbody > :nth-child(1) > :nth-child(6)').click()
          } else if (
            Cypress.$(':nth-child(2) > :nth-child(6) > .index_textBlock_20DCh').eq(0).text().length == 0 &&
            Cypress.$(':nth-child(2) > :nth-child(7) > .index_textBlock_20DCh').eq(1).text().length == 0 &&
            Cypress.$(':nth-child(2) > :nth-child(8) > .index_textBlock_20DCh').eq(2).text().length == 0
          ){
            //Do Something
            cy.get('tbody > :nth-child(2) > :nth-child(6)').click() }
//another more conditions

在这里,我正在检查块为空(否则转到elseIf),但是它一开始已经失败了(错误:应该是空的,但是找到了一些东西)。那么,我的问题是,这个代码有什么问题,柏树是否支持使用&&?

HTML:

代码语言:javascript
运行
复制
<tr class = ="index_tr_2P">
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">AnyText</div></td>
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">SomeText</div></td>
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">MoreTextHere</div></td>
</tr>

<tr class = ="index_tr_2P">
    <td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">AnyText2</div></td>
    <td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">SomeText2</div></td>
    <td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">MoreTextHere2</div></td>
    </tr>
EN

Stack Overflow用户

发布于 2021-10-13 18:28:42

你可以做这样的事。

代码语言:javascript
运行
复制
if (
  Cypress.$('td div.index_textBlock_20DCh').eq(0).is(':empty') &&
  Cypress.$('td div.index_textBlock_20DCh').eq(1).is(':empty') &&
  Cypress.$('td div.index_textBlock_20DCh').eq(2).is(':empty')
) {
  //Do Something
} else {
  //Do Something
}

您还可以提取文本并检查文本长度是否为0。类似于:

代码语言:javascript
运行
复制
if (
  Cypress.$('td div.index_textBlock_20DCh').eq(0).text().length == 0 &&
  Cypress.$('td div.index_textBlock_20DCh').eq(1).text().length == 0 &&
  Cypress.$('td div.index_textBlock_20DCh').eq(2).text().length == 0
) {
  //Do Something
} else {
  //Do Something
}

使用each()

代码语言:javascript
运行
复制
var countWithoutText = 0
cy.get('td div.index_textBlock_20DCh')
  .each((ele, lis) => {
    if (ele.text().length == 0) {
      countWithoutText++
    }
  })
  .then((lis) => {
    if (lis == countWithoutText) {
      //Do something all elements are empty
    } else {
      //Do something when all elements are not empty
    }
  })
票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69554607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档