英文:
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("//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(),'*')]"));
I am trying to store this using the below line but it is giving an error.
List<WebElement> ExpList=new ArrayList<WebElement>() {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<WebElement> 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<Object> 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<Object> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论