-1

I have webelement

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

I want to convert WebElement element into list

How can i convert it into list

I dont want to declare findelements again to get list. I need to reuse the webelement(element)

supputuri
  • 13,644
  • 2
  • 21
  • 39
Kriez
  • 49
  • 1
  • 13

3 Answers3

2

Just change the line to below, where we are using .findElements() which will return a list of elements rather than .findElement().

List<WebElement> elements = driver.findelements(By.cssSelector("div#name"));
JeffC
  • 22,180
  • 5
  • 32
  • 55
supputuri
  • 13,644
  • 2
  • 21
  • 39
1

You can use 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 );
Aleks D
  • 366
  • 5
  • 10
1

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:

List<WebElement> elements = new ArrayList<>();
elements.add(driver.findElement(By.cssSelector("div#name")));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352