谓词硒过滤器 [英] selenium filter with Predicate
本文介绍了谓词硒过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望使用和了解谓词和Lambda表达式.特别是与硒一起使用.
I am looking to use and learn about the Predicate and Lambda expression. In particular using it with selenium.
想象一下,您有大量的WebElements(列表)选择,并且您希望对其应用谓词过滤器,以使列表更小.
Imagine you have a large selection(List) of WebElements and you want to apply a predicate filter to it so that the list is smaller .
我在1,2,3的正确轨道上,我会做出哪些改变?
Am I on the right track for 1,2,3 below what changes would I meed to make?
List<WebElement> webElements = driver.findElements(By.cssSelector(".someClassName")); // returns large list
// then try and filter down the list
Predicate<WebElement> hasRedClass -> Predicate.getAttribute("class").contains("red-background");
Predicate<WebElement> hasDataToggleAttr -> Predicate.getAttribute("data-toggle").size() > 0;
// without predicate probably looks like this
//driver.findElements(By.cssSelector(".someClassName .red-background"));
// 1. this is what I think it should look like???
List<WebElement> webElementsWithClass = webElements.filter(hasRedClass);
// 2. with hasDataToggleAttr
List<WebElement> webElementsWithDataToggleAttr = webElements.filter(hasDataToggleAttr);
// 3. with both of them together...
List<WebElement> webElementsWithBothPredicates = webElements.filter(hasRedClass, hasDataToggleAttr);
推荐答案
我希望这里是您要寻找的东西:
I hope here is what you are looking for:
List<WebElement> webElements = driver.findElements(By.cssSelector(".someClassName")); // returns large list
// then try and filter down the list
Predicate<WebElement> hasRedClass = we -> we.getAttribute("class").contains("red-background");
Predicate<WebElement> hasDataToggleAttr = we -> we.getAttribute("data-toggle").length() > 0;
// without predicate probably looks like this
//driver.findElements(By.cssSelector(".someClassName .red-background"));
// 1. this is what I think it should look like???
List<WebElement> webElementsWithClass = webElements.stream()
.filter(hasRedClass).collect(Collectors.toList());
// 2. with hasDataToggleAttr
List<WebElement> webElementsWithDataToggleAttr = webElements.stream()
.filter(hasDataToggleAttr).collect(Collectors.toList());
// 3. with both of them together...
List<WebElement> webElementsWithBothPredicates = webElements.stream()
.filter(hasDataToggleAttr.and(hasRedClass)).collect(Collectors.toList());
这篇关于谓词硒过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文