简体   繁体   English

如何将webelement转换为List<Webelement> 在 Java 中

[英]How to convert webelement into List<Webelement> in java

I have webelement我有网络元素

WebElement element = driver.findelement(By.cssSelector("div#name"))

I want to convert WebElement element into list我想将 WebElement 元素转换为列表

How can i convert it into list我怎样才能把它转换成列表

I dont want to declare findelements again to get list.我不想再次声明 findelements 来获取列表。 I need to reuse the webelement(element)我需要重用 webelement(element)

只需将行更改为下面,我们使用.findElements()将返回元素列表而不是.findElement()

List<WebElement> elements = driver.findelements(By.cssSelector("div#name"));

You can use java.util.Collections.singletonList.您可以使用 java.util.Collections.singletonList。

And your code can be so:你的代码可以是这样:

WebElement element = driver.findelement(By.cssSelector("div#name"));
List<WebElement> elements = java.util.Collections.singletonList(element );

Java is a statically typed , so to before putting the WebElement into the list, you have to create the list first and then add the WebElement within the list and you can use the following solution: Java 是静态类型的,因此在将WebElement放入列表之前,您必须先创建列表,然后在列表中添加WebElement ,您可以使用以下解决方案:

List<WebElement> elements = new ArrayList<>();
elements.add(driver.findElement(By.cssSelector("div#name")));

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

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