简体   繁体   English

如何迭代多个列表 <WebElement> 在硒/ Java中

[英]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.

相关问题 Java:无法遍历Selenium中的WebElement列表 - Java: Cannot Iterate Through WebElement List in Selenium Selenium webdriver - 迭代,找到 webelement 然后点击它 - 我该怎么做? - Selenium webdriver - Iterate, find webelement and then click on it - how can I do that? Selenium 和 Java:如何在 WebElement 之后获取所有文本 - Selenium and Java: How do I get all of the text after a WebElement 如何确定Selenium是否存在WebElement? - How do I determine if a WebElement exists with Selenium? 我如何在Selenium Webdriver中找到此webelement? - How do i locate this webelement in Selenium Webdriver? 硒WD | 如何将所有值从WebElement列表复制到字符串列表? - Selenium WD | How do I copy all values from a WebElement list into a String list? 当Select和WebElement不起作用时,如何在Selenium WebDriver中打开列表并选择一个选项 - How do I open a list and select an option in Selenium WebDriver when Select and WebElement do not work 如何从ArrayList Selenium WebDriver JAVA中删除重复的WebElement? - How do I remove repeated WebElement from ArrayList Selenium WebDriver JAVA? 如何放置清单 <webelement> 使用硒网络驱动程序/ Java进入ArrayList? - How to put List<webelement> into ArrayList using selenium web driver/java? 如何将webelement转换为List<Webelement> 在 Java 中 - How to convert webelement into List<Webelement> in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM