我想从网页上的表格中的一列中获得所有的值,直到该列不包含元素,求和值,并返回和作为承诺。
TablePage.prototype.getPriceValuesFromList = function () {
for (number = 1; number < 100; number++) {
var locator = '//*[@id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
browser.findElement(By.xpath(locator)).then(function (success) {
if (success) {
var locator = '//*[@id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
return element(By.xpath(locator)).getText().then(function (text) {
numb = text.replace(/,/g, '');
numb = numb.replace(/\$/g, '');
numb = numb.replace(/\./g, '');
prices[number] = parseInt(numb);
console.log(prices[number]);
var sum = prices.reduce(function(a, b) { return a + b; }, 0);
console.log(sum);
return sum
})
}
else {
break;
}
})
}
};
我得到:“非法中断声明”
到底该怎么做呢?
我需要定义两次"locator“吗?
发布于 2018-09-23 06:26:19
您可以使用element.all()
查找第三列的所有td
,然后使用map()
将td
的每个文本值转换为Number
。
TablePage.prototype.getPrice2List = function(){
return element
.all(by.css('#root table tbody tr > td:nth-child(3)'))
.map(function(item){
// replace all non-numeric,non-dot, not-dash to ''
return Number(item.replace(/[^\d\.-]/g, '').trim());
});
};
tablePage.getPrice2List().then(function(prices){
console.dir(prices);
})
https://stackoverflow.com/questions/52440966
复制相似问题