【问题标题】: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;
}