I have a List<RowObject>
for which I need to convert to WebElements
, then for each row find specific column, then find specific attribute and in the end - getText()
How to do it in streams? It doesn't accept what I'm trying to do:
List<WebElement> list1 = getsomeRowstList().stream()
.map(TableRow::getWebElement)
.collect(Collectors.toList()); //it works OK
then
List<String> list2 = list1.stream()
.filter(a -> a.findElements(By.cssSelector("locator-for-specific-column-with-parameter"))
.stream().filter(m -> m.findElements(By.cssSelector("row-index-locator"))).map(WebElement::getText)
.collect(Collectors.toList()));
It does not accept it and underlines the second findElements
saying:
Bad return type in lambda expression: List<WebElement> cannot be converted to boolean
I'm quite new to streams
filter()
is an intermediate operation, so stream().filter();
does not work (see the formatting below).
List<WebElements> list2 = list1.stream()
.filter(
a -> a.findElements(By.cssSelector("locator-for-specific-column-with-parameter"))
.stream().filter(m -> m.findElements(By.cssSelector("row-index-locator"))).map(WebElement::getText)
.collect(Collectors.toList())
);
Your return type is List<String> list2
. I recommend you to do the filtering first (gives you the specific WebElement
), then convert it with map
( WebElement::getText
).
I haven't tested but it should be something like this:
List<String> list2 = list1.stream()
.filter(a -> a.findElements(By.cssSelector("locator-for-specific-column-with-parameter")).stream()
.filter(m -> m.findElements(By.cssSelector("row-index-locator"))))
.map(WebElement::getText).collect(Collectors.toList());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.