How to convert WebElement List to String List in Selenium Java

Convert WebElement List to String List in Selenium using Java. In the below Selenium with Java Example let’s create a java method. that will get a list of web elements as input parameters and return the list of strings. Converting List<WebElement> to List<String>

Convert WebElement List to String List in Selenium Java

public List<String> WebelementToString(List<WebElement> elementList){

List<String> stringList = new ArrayList<String>();

for (WebElement element : elementList ) {

stringList.add(element.getText().toString());
System.out.println(element.getText());
}

for(String s : stringList) {
System.out.println("Element of the list into String formate");
System.out.println(s);
}

return stringList;
}

WebelementToString() is a custom method that will take a parameter as WebElement List<WebElement> elementListIt will get the text of each WebElement from the web element list and add these elements into another list of type String and return the List of string return stringList;

Verify list into selenium Java

Let’s create an automation script to read the list of text from the webpage and verify some text from on the page. Selenium always reads the HTML as a type of web element but to verify we have to get the text from the web elements and convert them into a string.

Testcase to verify a String List

Given User navigate to URL “http://the-internet.herokuapp.com/”.

When User read the element list.

Then User can see A/B testing into list.

package com.Automation;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import com.sun.tools.javac.util.Assert;

public class App {
 
static WebDriver driver;

@Test
public static void main(String[] args) {
 
 try {
  
  System.setProperty("webdriver.chrome.driver", "D:\\Driver\\95\\chromedriver.exe");
  driver=new ChromeDriver();
  driver.manage().window().maximize();
  //driver.manage().deleteAllCookies();
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
  
  driver.navigate().to("http://the-internet.herokuapp.com/");
   
  List<WebElement> elementList = driver.findElements(By.xpath("//*[@id='content']/ul/li[*]/a"));

  List<String> strlist  = WebelementToString(elementList);
  
  assertTrue(strlist.contains("A/B Testing"));
  
  } catch (Exception e) {
  e.printStackTrace();
  }


}

public static List<String> WebelementToString(List<WebElement> elementList){
 
 List<String> stringList = new ArrayList<String>();  

 for (WebElement element : elementList ) {
  
  stringList.add(element.getText().toString());
   //System.out.println(element.getText());
    }

 return stringList;
}
}