[英]How do I iterate multiple List<WebElement> in selenium /Java
I have the following WebElement List in a page 我在页面中有以下WebElement列表
@FindBy(xpath="a")
private List<WebElement> generalList ;
@FindBy(xpath="b")
private List<WebElement> eventList ;
@FindBy(xpath="c")
private List<WebElement> additionalList ;
I want to iterate each of the above list items and apply clear through a loop instead of writing the following way which currently works 我想遍历上述每个列表项并通过循环应用清除,而不是编写以下当前有效的方式
for(WebElement type:generalList)
type.clear();
for(WebElement type:eventList)
type.clear();
for(WebElement type:additionalList)
type.clear();
In Java 8 you can try using Stream
as below :- 在Java 8中,您可以尝试使用
Stream
,如下所示:-
generalList.stream().forEach(elem -> elem.clear());
eventList.stream().forEach(elem -> elem.clear());
additionalList.stream().forEach(elem -> elem.clear());
Or concatenate all of these list and try as : 或合并所有这些列表,然后尝试作为:
Stream.of(generalList, eventList, additionalList).flatMap(Collection::stream).forEach(elem -> elem.clear());
With Guava: 用番石榴:
for (WebElement type : Iterables.concat(generalList, eventList, additionalList)) {
type.clear();
}
With Java 8: 使用Java 8:
Stream.of(generalList, eventList, additionalList)
.flatMap(List::stream)
.forEach(WebElement::clear);
you can create a helper function 您可以创建一个辅助函数
void clearAll(List<WebElement>... f) {
Arrays.stream(f).forEach((li)->{li.forEach(WebElement::clear);});
}
and call it like clearAll(generalList, eventList, additionalList);
并像
clearAll(generalList, eventList, additionalList);
一样调用它clearAll(generalList, eventList, additionalList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.