我正在尝试获取表的行: 而且我正在计算行数: 我需要获取Promise返回的值并增加另一个值: 一个+ rowCountWhile>在同一函数的while循环中。 无法获取rowCountWhile的值进行递增
public getRowCount(webTableElement:ElementArrayFinder,totalcount:ElementFinder){
let finalTotal
let totalslice
let finalrowCount
let rowCountWhile1
totalcount.getText().then(function(total){
console.log("total:"+total)
totalslice = total.slice(15);
finalTotal = parseInt(totalslice);
console.log("final total:"+finalTotal);
let countrows = webTableElement.count().then(function(rowCount){
console.log("tr count:"+rowCount)
finalrowCount = rowCount;
console.log("Final RowCount:"+finalrowCount)
while(finalrowCount<finalTotal)
{
console.log("Test"+finalrowCount)
provisioningToolPage.superBranchIDNextNavigator.click();
return webTableElement.count().then(function(rowCountWhile){
console.log("Promise value:"+rowCountWhile)
rowCountWhile1 = rowCountWhile;
return rowCountWhile1
})
console.log("outside promise:"+rowCountWhile1)
//finalrowCount = finalrowCount + rowcountwhile;
//console.log("rowCountWhile:"+finalrowCount1)
finalrowCount= finalrowCount + 10;
}
})
})
}
答案 0 :(得分:0)
关注代码:
while(finalrowCount<finalTotal)
{
console.log("Test"+finalrowCount)
provisioningToolPage.superBranchIDNextNavigator.click();
return webTableElement.count().then(function(rowCountWhile){
console.log("Promise value:"+rowCountWhile)
rowCountWhile1 = rowCountWhile;
return rowCountWhile1
})
console.log("outside promise:"+rowCountWhile1)
//finalrowCount = finalrowCount + rowcountwhile;
//console.log("rowCountWhile:"+finalrowCount1)
finalrowCount= finalrowCount + 10;
}
})
您正在将while
与Promise
混合。将会发生的是,{em> all while
语句将在第一个承诺解决之前 执行。
最简单的解决方法是使用async/await
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function