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 in Selenium by Surya
• 970 points
2,377 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 13 by Vaishnavi
• 1,160 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
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,014 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,158 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
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 in Selenium by Vaishnavi
• 1,160 points

edited May 10 by Omkar 1,238 views