How to print text from a list of all web elements with same class name in Selenium

0 votes
How to print text from a list of all web elements with same class name in Selenium?
Jun 20, 2019 in Selenium by Rajni
87,730 views

3 answers to this question.

0 votes

@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.

answered Jun 20, 2019 by Avantika

Thanks, its working... can you please explain the below for loop condition. 

for (WebElement webElement : bakeries) 

This is an enhanced for loop (also called as for each loop)

Syntax: for( type var : collection)

So here you have a List (bakeries) and you are iterating through the list using a variable(webElement) of type WebElement.

Hope you are clear!

Thank you ..it's working !
It's not working for me

code: ans=driver.find_elements_by_xpath('//span[@class="_1kMS"]')

i want to extract the text inside it

Hello @Meethi

What error you are getting? And please share your complete code so that I can help you out?

0 votes

Text() Method of Selenium

  1. Open Firefox browser with the URL: SoftwareTestingHelp.com.
  2. Using text method of selenium web driver, find the web element with text – Write and Earn.
  3. Validate if the selected element is displayed on the web page.
  4. If it is displayed, print the text as Element found using text.

    For details, You can even check out Selenium concepts with the Selenium online course.
answered Dec 16, 2020 by Gitika
• 65,770 points
0 votes

Try using List <WebElement> to access all similar elements :

List<WebElement> listElement = driver.findElements(By.className("human-readable"));
for(int i =0;i<listElement.size();i++) {
 String elementText = listElement.get(i).getText(); 
 System.out.println(elementText); 
}
answered Dec 16, 2020 by Roshni
• 10,480 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

Getting a text of elements in div tag having same class name

Basically,there is no need to retrieve element ...READ MORE

answered May 17, 2018 in Selenium by Samarpit
• 5,910 points
22,870 views
+1 vote
1 answer

How to handle a combobox which when clicked open in new window with list of items(to be clicked) in selenium java ?

@Pavithra, you can try using following commands ...READ MORE

answered Oct 29, 2019 in Selenium by Abha
• 28,140 points
1,524 views
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,776 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,191 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,267 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,904 views
0 votes
1 answer
0 votes
1 answer

How to extract text from a web page using selenium and save it as a text file?

Hello Isha, you can checkout this code ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
33,767 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP