How can Selenium select each div separately that have the same class

+1 vote

I am trying to select all the divisions with class tile-consultation, click each of them and extract some data from every div in the same class, I tried using:

    List<WebElement> professors = driver.findElements(By.className("tile-consultation"));
    ListIterator<WebElement> theListOfProfessors = professors.listIterator();
    Thread.sleep(1000);

    int i = 1;
    while(theListOfProfessors.hasNext()) {
        WebElement professorI = driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(2)"));
        professorI.click();

        Thread.sleep(1000);
        close = driver.findElement(By.cssSelector("button.btn-close"));
        close.click();
        Thread.sleep(1000);
     }

But, is it possible to change 1 to the 2nd, 3d and so on in a while loop?

Mar 13, 2019 in Selenium by Surya
• 970 points
24,367 views

1 answer to this question.

+1 vote

Hi Surya, you have already got the work done. The web elements are already loacted and made a list iterator here:

List<WebElement> professors = driver.findElements(By.className("tile-consultation"));
ListIterator<WebElement> theListOfProfessors = professors.listIterator();

The findElements method will return a collection of elements that match with the selector. There is no need for you to retrieve the elements again like you are trying to using this driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(x)" inside that loop. You merely need to iterate over the list iterator theListOfProfessors that you have already created. E.g. something to the effect of

while(theListOfProfessors.hasNext()) {
    WebElement elem = theListOfProfessors.next()
    // do something with element
}

Hope this was helpful!

answered Mar 14, 2019 by Vaishnavi
• 1,180 points

thanks Vaishnavi! really helpful!!

Hi 

I am doing some action on https://www.makemytrip.com/ under Departure. When click on Departure, it will show calendar of two months. 

I am able to get two webElements with same xpath as mentioned below, now I want to go one by one, and getText() to match with user month name.

List<WebElement> month_list= driver.findElements(By.xpath("//div[@class='DayPicker-Month']"));

ListIterator<WebElement> listIter= month_list.listIterator();

while(listIter.hasNext()) {

WebElement elem= listIter.next();

System.out.println(elem.findElement(By.xpath("//div[@class='DayPicker-Caption']/div")).getText());

}

But everytime, it is reporting same text " July 2019".

July 2019
July 2019

 However, I am expecting it should report 

July 2019 

August 2019

Please guide me how can I perform anything on each element one by one.

Hey Suman, instead of locating datepicker-Month class, try to find datepicker-caption class directly and loop though it to getText() to match with user-month name. Following code would help you:

driver.get(" https://www.makemytrip.com/");

driver.findElement(By.xpath("//span[contains(text(),'DEPARTURE')]")).click();

List<WebElement> month_list= driver.findElements(By.xpath("//div[@class='DayPicker-Caption']"));

for (WebElement webElement : month_list) {

System.out.println(webElement.getText());

}

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How can I perform multiple selection of options in a dropdown using Select class in Selenium?

Hey Priyansh, you can select multiple options ...READ MORE

answered Jul 8, 2019 in Selenium by Anvi
• 14,150 points
2,924 views
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
12,711 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
8,607 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
6,680 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,551 views
0 votes
1 answer

How do I increase or decrease the sleep time in clickAndWait command in selenium IDE?

If you are using Selenium Webdriver, you have ...READ MORE

answered Feb 28, 2019 in Selenium by Vaishnavi
• 1,180 points
2,192 views
0 votes
1 answer

How to auto-refresh the ChromeDriver using Selenium Webdriver?

You can use this command. Also, refresh ...READ MORE

answered May 10, 2019 in Selenium by Vaishnavi
• 1,180 points

edited May 10, 2019 by Omkar 13,237 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