Get text from List<WebElement> in Groovy

依然范特西╮ 提交于 2019-12-24 12:10:06

问题


I am dealing with problem to implement groovy lambda function getting text from List collection into List collection. The original java code:

list.stream().map(WebElement::getText).collect(Collectors.toList());

My Groovy version fails:

list.stream().map({ WebElement } as String).collect(Collectors.toList())

groovy.lang.MissingMethodException: No signature of method: java.util.stream.ReferencePipeline$Head.map() is applicable for argument types: (java.lang.String) values: [quality1.CommonMethods$_clickSubMenuLeftBar_closure2@4e49ce2b] Possible solutions: map(java.util.function.Function), max(java.util.Comparator), min(java.util.Comparator), wait(), grep(), any() –

Can anybody help me how to make it working? I was trying to use similar approach like here: http://mrhaki.blogspot.com/2015/04/groovy-goodness-use-closures-as-java.html but without success.


回答1:


Groovy 3.0 (current version: 3.0.0-alpha-3) will support lambda expressions and method references thanks to a new parrot parser - http://groovy-lang.org/releasenotes/groovy-3.0.html

For Groovy 2.5.x and older you will have to replace method reference:

WebElement::getText

with a closure:

{ el -> el.getText() }

Final working example should look like this:

list.stream().map{ el -> el.getText() }.collect(Collectors.toList())


来源:https://stackoverflow.com/questions/51634927/get-text-from-listwebelement-in-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!