@Rajni, following test case is somewhat similar to your problem. Try using this code snippet to resolve your issue:
public static WebDriver driver;
public static String driverPath = ".\\chromedriver.exe";
public static void main(String[] args) {
// Setting up Chrome driver path.
System.setProperty("webdriver.chrome.driver", driverPath);
// Launching Chrome browser.
driver = new ChromeDriver();
driver.get("https://www.justdial.com/Bangalore/Bakeries");
List<WebElement> bakeries = driver.findElements(By.className("store-name"));
System.out.println(bakeries.size());
for (WebElement webElement : bakeries) {
String name = webElement.getText();
System.out.println(name);
}
}
In the above code, I have a webpage with a list of bakeries in the area. Now for all the list elements, the class-name is same i.e. "store-name". So I have used a List to store all of these elements and then iterate through the list to print name of each item.
For further understanding, you can refer to the Selenium Certification.