0

I am trying to select all the divs withclass tile-consultation in (http://raspored.finki.ukim.mk/Home/Consultations), click each of them and extract some data from each div, 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 how can I change 1 to the 2nd, 3d and so on in a while loop?

sytech
  • 29,298
  • 3
  • 45
  • 86
M.T
  • 303
  • 4
  • 15
  • 1
    You probably want to use the `findElements` (plural) methods which should return a collection of all the elements matching the selector. Then simply drop the nth-of-type from the selector and it should match them all. – sytech Mar 20 '18 at 16:32
  • I did, I will add the rest of the code. – M.T Mar 20 '18 at 16:34
  • Yeah, but I'll have to click each of the div elements and extract some data from each div. – M.T Mar 20 '18 at 16:35
  • 1
    I'm not familiar with the language you're using, but there should be no need for you to do `driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(x)"))` at all. Why are you doing this? You should already have all of the web welements in `theListOfProfessors` -- you should iterate over that rather than trying to retrieve the web elements again. – sytech Mar 20 '18 at 16:36
  • I'm using Java, I did that just so that somehow I'll keep track of the number of divs in the site. – M.T Mar 20 '18 at 16:38
  • Is there a way for me to add some variable instead of a number in `driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(X)"))`? – M.T Mar 20 '18 at 16:39
  • 1
    I'm not familiar with Java, but my best guess would be something like this (pseudo code) `while theListOfProfessors.hasNext(): WebElement elem = theListOfProfessors.next() ...` -- You already have the [listiterator](https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html) object, `theListOfProfessors` you just need to use it! – sytech Mar 20 '18 at 16:47
  • Can you update the question with the relevant _HTML_ and the exact _Manual Steps_ you are trying to _Automate_? – undetected Selenium Mar 20 '18 at 17:28
  • @sytech, thank you, that actually worked! – M.T Mar 20 '18 at 17:30
  • 1
    Glad to hear @M.T -- I've formulated this into an answer. If it worked for you, please consider accepting it as the answer to this question. – sytech Mar 20 '18 at 17:41
  • Possible duplicate of [Ways to iterate over a list in Java](https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java) – SiKing Mar 20 '18 at 17:52

1 Answers1

4

You've already got the work done. You've already found the web elements and made a listiterator here:

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

The findElements method will return a collection of elements matching the selector. There's no need for you to retrieve the elements again like you're trying to with driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(x)" inside that loop. You merely need to iterate over the listiterator theListOfProfessors that you already created. E.g. something to the effect of

while(theListOfProfessors.hasNext()) {
    WebElement elem = theListOfProfessors.next()
    // do something with elem
}
sytech
  • 29,298
  • 3
  • 45
  • 86