简体   繁体   English

如何在列表中找到元素(尽快) <WebElement>

[英]How to find element(as fast as possible) in List<WebElement>

can you, please, help me? 你能帮我么?

I have a List and its size about 200 elements. 我有一个列表,它的大小约为200个元素。 My code is taking about 10 seconds to find right element(if it placed at the end of list). 我的代码大约需要10秒钟才能找到正确的元素(如果放置在列表的末尾)。 And i am seeking a faster way to find it. 我正在寻找一种更快的方法来找到它。 Thanks in advance(and sorry if i made rude mistakes in english) 在此先感谢您(如果我在英语上犯了粗鲁的错误,对不起)

for (int i = 0; i < ToRecObj.List.size(); i++) {            
        WebElement iter = ToRecObj.List.get(i);
        iter.click();
        String iterValue = iter.getText().trim();
        if (iterValue.equals(someStringForSearch)) {
            MainPage.SelectListField.click();
            break;
        }
    }

Maybe List.indexOf() is what you are looking for. 也许List.indexOf()是您想要的。 It returns the index of the first appearance of an element (if there are duplicates) As you dont describe what the could should do and what the classes do i can only guess what you want to do: You want to search all of your WebElements for a specific serch string. 它返回元素首次出现的索引(如果存在重复项),因为您没有描述容器应该做什么以及类应该做什么,所以我只能猜测您想做什么:您想搜索所有WebElements特定的Serch字符串。
With Java 8 you could do: 使用Java 8,您可以执行以下操作:

if(ToRecObj.List.stream().anyMatch(we -> we.getText().trim().equals(someStringForSearch)) {
   MainPage.SelectListField.click();
}

Using anyMatch() it will return true also if there is more than one match to your search string. 如果对您的搜索字符串有多个匹配项,则使用anyMatch()也会返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM