- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can Selenium select each div separately that have the same class?
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.
Syntax
List<WebElement> l = driver.findElements(By.className("store-details"));
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class DivSameCls{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.justdial.com/Bangalore/Bakeries"); // 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: "); System.out.println(v); } //browser close driver.close(); } }
Output
- Related Articles
- How can I select date from a datepicker div using Selenium Webdriver?
- Can a method have the same name as the class?
- Find div element by multiple class names in Selenium?
- What are the various methods available under Select class in Selenium?
- How will you select a particular value in a dropdown without using the methods of Select class in Selenium?
- Can different HTML elements have the same ID?
- MySQL Select Rows where two columns do not have the same value?
- How to return rows that have the same column values in MySQL?
- How can I show a hidden div when a select option is selected in JavaScript?
- Get the text from multiple elements with the same class in Selenium for Python?
- How can I show figures separately in Matplotlib?
- Should a constructor always have the same name as the class in java?
- How many public classes of the same name it can have in Java?
- How many destructors can we have in one class in C#?
- How can I select an element by its class name using jQuery?

Advertisements