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
11,129 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 !

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,870 points
12,076 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,920 points
260 views
0 votes
1 answer
0 votes
1 answer

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,510 points
2,552 views
0 votes
1 answer
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,870 points
2,028 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
2,116 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,030 points
13,847 views