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
22,128 views

1 answer 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.

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?

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,890 points
15,899 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
• 27,950 points
339 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,520 points
4,017 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31 in Selenium by Sri
• 2,970 points
4,963 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,890 points
2,905 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 2 in Selenium by Sri
• 2,970 points
3,331 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,110 points
22,024 views