- 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 to print text from a list of all web elements with same class name in Selenium?
We can get the text from a list of all web elements with the same class name in Selenium webdriver. We can use any of the locators like the class name with method By.className, xpath with method By.xpath, or css with method By.cssSelector.
Let us verify a xpath expression //h2[@class='store-name'] which represents multiple elements that have the same class name as store-name. If we validate this in Console with the expression - $x("//h2[@class='store-name']"), it yields all the matching elements as shown below:
Also, since we need to obtain multiple elements, we have to use the findElements method which returns a list. We shall iterate through this list and obtain the text of the elements with getText method.
Syntax
List<WebElement> m = driver.findElements(By.xpath("//h2[@class='store-name']"));
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 ElementsSameclsname{ 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 elements list with same class name List<WebElement> m = driver.findElements(By.xpath("//h2[@class='store-name']")); // iterate over list for(int i = 0; i< m.size(); i++) { //obtain text String s = m.get(i).getText(); System.out.println("Text is: " + s); } driver.quit(); } }
Output
- Related Articles
- Get the text from multiple elements with the same class in Selenium for Python?
- How to get text with selenium web driver in python?
- How to extract text from a web page using Selenium and save it as a text file?
- List down the name of the Web drivers supported by Selenium.
- How to use for loop to print all the elements of a list in R?
- How to determine if all elements are the same in a Java List?
- How to extract text from a Javascript alert in Selenium with python?
- Python - Check if all elements in a List are same
- Get text using selenium web driver in python?
- How to identify multiple elements at the same time in Selenium with python?
- Clear text from textarea with selenium.
- How to get css class name using Selenium?
- How to get entered text from a textbox in selenium?
- How to apply CSS style to the different elements having the same class name in HTML?
- How to read a text file in Selenium with python?

Advertisements