我正在尝试使用array.map函数来查找存储在数组中的所有Web元素的文本属性。 有没有办法处理这个问题并重试查找文本属性?
具有用于重试的正常 for 循环的标准解决方案是一种选择。想检查替代解决方案。
let name_planogram_table = await element.all(by.xpath(planograms_Table_cart_Category));
let uistringvaluearray = await Promise.all(name_planogram_table .map(name=> name.getText()));
量角器的each
应该可以解决问题。
let uistringvaluearray = await name_planogram_table.each(async arrayElement => {
return arrayElement.getText()
//This is just a quick example
//return the value, assign to another variable or do whatever you need to do
})
你应该能够通过在你的elementFinderArray上调用getText((来获取这些值,就像这样
let name_planogram_table = await element.all(by.xpath(planograms_Table_cart_Category)).getText();
console.log(name_planogram_table);
它将生成一个字符串数组。
-
await
关键字解析承诺。element.all(by.xpath(planograms_Table_cart_Category))
不是承诺,没有必要await
它 -
如上所述,这将起作用
let name_planogram_table = await element.all(by.xpath(planograms_Table_cart_Category)).getText();
console.log(name_planogram_table);
// output
// [
// "value1",
// "value2",
// "value3"
// ]
但!!!一年前有一个错误(也许它仍然存在(,当您针对很多元素(30、50、100+(调用.getText()
或.getAttribute()
时,您会收到stale element
错误。在这种情况下,唯一的解决方案是for
循环