How can Selenium select each div separately that have the same class?

SeleniumAutomation TestingTesting Tools

We can select each div separately that have the same class with the help of the Selenium webdriver. Often in the html code, we find more than one div element having a class attribute with the same value.

Let us see the html code of elements with div tag having the same value set for the class attribute (as highlighted in the image). The value of the class attribute is - colsm-5 col-xs-8 store-details sp-detail paddingR0.

To identify each of these div elements separately, we shall use the method findElements and pass the value of the class attribute as a parameter to this method. The findElements method returns a list of matching elements. We have to iterate through this list and identify each div separately.


List<WebElement> l =


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import java.util.List;
public class DivSameCls{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      // identify div elements with same class
      List<WebElement> l = driver.findElements(By.className("store-details"));
      // list iteration
      System.out.println("Elements are: ");
      for(int j = 0; j< l.size(); j++) {
         //identify separate div
         String v = l.get(j).getText();
         System.out.println("Elements are: ");
      //browser close


Updated on 08-Apr-2021 07:35:37