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 in Selenium by Rajni
1,685 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 by Avantika

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,130 points
6,768 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 in Selenium by Abha
• 27,180 points
171 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,110 points
1,010 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,130 points
1,154 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,130 points
894 views
0 votes
1 answer