从 List<WebElement> 获取文本在 Groovy 中 [英] Get text from List&lt;WebElement&gt; in Groovy

查看:37
本文介绍了从 List<WebElement> 获取文本在 Groovy 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理实现 groovy lambda 函数从 List 集合获取文本到 List 集合的问题.原java代码:

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

我的 Groovy 版本失败:

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

<块引用>

groovy.lang.MissingMethodException: 没有方法签名:java.util.stream.ReferencePipeline$Head.map() 适用于参数类型:(java.lang.String) 值:[quality1.CommonMethods$_clickSubMenuLeftBar_closure2@4e49ce2b]可能的解决方案:地图(java.util.function.Function),最大值(java.util.Comparator), 最小值(java.util.Comparator), 等待(), grep(),any() –

有人可以帮助我如何使它工作吗?我试图使用类似的方法:http://mrhaki.blogspot.com/2015/04/groovy-goodness-use-closures-as-java.html 但没有成功.

解决方案

Groovy 3.0 (当前版本:3.0.0-alpha-3) 将支持 lambda 表达式和方法引用,这要归功于新的 parrot解析器 - http://groovy-lang.org/releasenotes/groovy-3.0.html

对于 Groovy 2.5.x 及更早版本,您必须替换方法引用:

WebElement::getText

有一个闭包:

{ el ->el.getText() }

最终的工作示例应如下所示:

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

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.

解决方案

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())

这篇关于从 List<WebElement> 获取文本在 Groovy 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆