Home > Enterprise >  How to store text of all web elements and then print the text of all the elements
How to store text of all web elements and then print the text of all the elements

Time:11-04

 List<WebElement> GroupList = driver.findElements(By.xpath("//*[Abc']"));
      
   for (WebElement element: GroupList){
            logger.info(element.getText());
     }

Current Code is just printing the text of all elements one by one. I want to store the texts in a single List and then print that list, Further i want to print text in this way "Abc, def, ghi" every element has comma and space between them and the last element would just print the element text and nothing else.

CodePudding user response:

This should do the job:

List<String> textList = new ArrayList<String>();
String[] xpathStrings = {"//*[Abc']", "//*[def']", "//*[ghi']"};
for (String xpathString: xpathStrings) {
   List<WebElement> elements = driver.findElements(By.xpath(xpathString));
   for (WebElement element: elements) {
      textList.add(element.getText().replace("'", ""));
   }
}
logger.info(textList);
logger.info(String.join(", ", textList));

CodePudding user response:

List<String> textList = new ArrayList<String>();
        List<WebElement> GroupList = driver.findElements(By.xpath("(//ABC)"));

        for ( WebElement element: GroupList) {
            {
                textList.add(element.getText());
            }
        }
        logger.info(textList);
        logger.info(String.join(", ", textList));
  • Related