Skip to content
Advertisement

How to add a web elements to an ArrayList?

I have a method in which I am trying to add 12 web elements:

JavaScript

Simplified DOM elements in which I only need to get the text :

JavaScript

I need to add all 12 web elements to my array and then make sure that the received elements contain the name “Iphone”, but when adding elements, there is exception:

JavaScript

Advertisement

Answer

iphoneSnippetList is a List of WebElement in Java-Selenium bindings.

I am not sure why you want to add 12 web elements using the loop, instead a findElements with right xpath would have been a good choice. Anyway, there’s a problem with you code related to casting.

See below, driver.findElement will return the web element, and we are storing that into a variable called Webelement e, and adding the same into iphoneSnippetList

JavaScript

Also, this loop will run for 13 times not 12 times. In case you want this to run for 12 times, initialize i = 1 not i = 0

I think you will have issue with xpath also, cause you are not using xpath indexing correctly.

try this instead :

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement