gpt4 book ai didi

javascript - 添加 Web 表定位器中所有值的总和,仅前两个定位器的方法结果值

转载 作者:行者123 更新时间:2023-12-03 02:31:41 24 4
gpt4 key购买 nike

我想添加网络表中定位器的所有值,但它只添加前两个值。下面是我的方法声明。

exports.GetMonthsFWSeasonSTDPreMkdValues = () => {
var total_value;
for(var month_index = 9; month_index <= 18 ; month_index++){
const elm_xpath = utils.GetXpathForSubCategory(chosen_season_index, month_index);
return browser.element(by.xpath(elm_xpath)).getText().then(function(response_total_months_index_values){
total_value += response_total_months_index_values;
console.log('total value' ,total_value);
});
}
};

最佳答案

根本情况是您在 For 循环中使用 return,因此循环仅迭代一次。

另一个隐藏的代码问题是 javascript 闭包问题,For 循环作为同步执行,但 getText() 内部循环作为异步执行。

如果删除关键字 returnbrowser.element(by.xpath(elm_xpath)).getText() 将重复使用 elm_xpath Month_index=18

exports.GetMonthsFWSeasonSTDPreMkdValues = () => {
var promises = [];

for(var month_index = 9; month_index <= 18 ; month_index++){
const elm_xpath = utils.GetXpathForSubCategory(chosen_season_index, month_index);
promises.push(element(by.xpath(elm_xpath)).getText());
}

return Promise.all(promises).then(function(data){

return data.reduce(function(accumulator, currentValue){
return accumulator + currentValue * 1;
}, 0);

});

};
//Remind: this function return a promise.

关于javascript - 添加 Web 表定位器中所有值的总和,仅前两个定位器的方法结果值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48708240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com