【问题标题】:Trying to count number of elements I get but returns zero试图计算我得到但返回零的元素数量
【发布时间】:2022-01-02 00:32:21
【问题描述】:

元素

<div>class="product-image-container"</div> 
<br>
<br> 

代码:

countMaterials(){  
        let countItems = 0;
        cy.get('#center_column').find("div").then((items) => {
             countItems = items.length;
        });
        return countItems;
}

我正在尝试让它动态化。

【问题讨论】:

    标签: cypress element variable-length


    【解决方案1】:

    您也可以使用别名来获取计数,然后稍后访问它,例如:

    cy.get('#center_column').find('div').its('length').as('itemLength')
    
    cy.get('@itemLength').then((itemLength) => {
      cy.log(itemLength) //prints itemLength
      // Access itemLength here
    })
    

    【讨论】:

      【解决方案2】:

      您已将countItems 转为array 并将push()items 转为array。最后,您返回countItems.length。它应该给你号码:

      countMaterials(){  
          let countItems = [];
      
          cy.get('#center_column').find("div").then((items) => {
              countItems.push(items);
          });
      
          return countItems.length;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-02-26
        • 1970-01-01
        • 2021-12-12
        • 2020-07-08
        • 1970-01-01
        • 2013-09-02
        • 2022-12-05
        • 2017-12-01
        • 1970-01-01
        相关资源
        最近更新 更多