如何将WebElements存储在一个数组中

huangapple go评论83阅读模式
英文:

How to store WebElements in an array

问题

// 以下是将上述的 webElements 存储到数组中的方法

WebElement NameLbl = driver.findElement(By.xpath("//h3[contains(text(),'Name')]"));
WebElement MobLbl = driver.findElement(By.xpath("//h3[contains(text(),'Mobile')]"));
WebElement LookingLbl = driver.findElement(By.xpath("//h3[contains(text(),'Looking for ')]"));
WebElement GenderLbl = driver.findElement(By.xpath("//h3[contains(text(),'Gender')]"));
WebElement MandatoryList = driver.findElement(By.xpath("//span[contains(text(),'*')]"));

List<WebElement> ExpList = new ArrayList<WebElement>(Arrays.asList(NameLbl, MobLbl, LookingLbl, GenderLbl));
英文:

I just wanted to know how to store the following webElements in an array.

 WebElement NameLbl = driver.findElement(By.xpath(&quot;//h3[contains(text(),&#39;Name&#39;)]&quot;));
    		WebElement MobLbl = driver.findElement(By.xpath(&quot;//h3[contains(text(),&#39;Mobile&#39;)]&quot;));
    		WebElement LookingLbl = driver.findElement(By.xpath(&quot;//h3[contains(text(),&#39;Looking for &#39;)]&quot;));
    		WebElement GenderLbl = driver.findElement(By.xpath(&quot;//h3[contains(text(),&#39;Gender&#39;)]&quot;));
    		WebElement MandatoryList = driver.findElement(By.xpath(&quot;//span[contains(text(),&#39;*&#39;)]&quot;));

I am trying to store this using the below line but it is giving an error.

List&lt;WebElement&gt; ExpList=new ArrayList&lt;WebElement&gt;() {NameLbl,MobLbl,LookingLbl,GenderLbl};

答案1

得分: 1

你不能像那样实例化列表。

你可能想要的是这样的:

List<WebElement> ExpList = List.of(NameLbl, MobLbl, LookingLbl, GenderLbl);

这将把你传递的参数转换为正确类型的列表。

英文:

You can't instantiate lists like that.

What you were probably going for was this:

List&lt;WebElement&gt; ExpList = List.of(NameLbl, MobLbl, LookingLbl, GenderLbl);

Which converts the arguments you've passed into the correct type of list.

答案2

得分: 0

我会建议另一种方法。。

你可以使用Robo Mop提出的方法,但是这样就不能后续修改列表。假设你有以下代码:

List<Object> objectList = Arrays.asList(new Object(), new Object());
objectList.add(new Object());
System.out.println(objectList);

上面的代码会抛出异常

>Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.AbstractList.add(AbstractList.java:153)
at java.base/java.util.AbstractList.add(AbstractList.java:111)

相反,我建议使用以下方法:

List<Object> objectList = Stream.of(new Object(), new Object()).collect(Collectors.toList());
objectList.add(new Object());
System.out.println(objectList);

这将创建类似于你在试验中使用的ArrayList对象。

英文:

I wound suggest another approach..

You can use the approach suggested by Robo Mop, however this won't let you to modify that list later. Assume you have the code:

List&lt;Object&gt; objectList = Arrays.asList(new Object(), new Object());
objectList.add(new Object());
System.out.println(objectList);

The code above would throw the exception

>Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.AbstractList.add(AbstractList.java:153)
at java.base/java.util.AbstractList.add(AbstractList.java:111)

Instead I would suggest to use this appraoch:

List&lt;Object&gt; objectList = Stream.of(new Object(), new Object()).collect(Collectors.toList());
objectList.add(new Object());
System.out.println(objectList);

This would create ArrayList object like you do you your trial.

huangapple
  • 本文由 发表于 2020年9月17日 18:06:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63935770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定