2

Is there an alternative way to replace 'Thread.sleep(5000)'? As currently, the commentsbutton only clicks once and will move on to the codes for repliesbutton even though there are other elements for commentsbutton? So i thought that it could be because of 'Thread.sleep(5000)' that the page is not done loading.

          if(commentsbutton.size() > 0) {
                commentsbutton.get(0).click(); //click on button if found
                Thread.sleep(5000); //pause for 5 seconds
          }           
          else clickMore = false;

          if(repliesbutton.size() > 0) {
                 repliesbutton.get(0).click(); //click on button if found
                 Thread.sleep(5000); //pause for 5 seconds
           }
           else clickMore = false; 

           if(seemorebutton.size() > 0) {
                  seemorebutton.get(0).click(); //click on button if found
                  Thread.sleep(5000); //pause for 5 seconds
           }
           else clickMore = false;
           }
dark_space
  • 179
  • 1
  • 2
  • 17

1 Answers1

0

You can use a loop for this, but you have to understand, that this loop will use process resources during the execution. I think, Thread.sleep is preffered in your case anyway...

long now = System.currentMillisec();   
long executionDuration = now + 5000;
while(now < executionDuration){
    now = System.currentMillisec();
}

But as you said

the commentsbutton only clicks once and will move on to the codes

It seems, that you need ta add additional loop over elements of commentsbutton collection, to click all the comments buttons you have before go to repliesbuttons. Now you are clicking only one button, which you get as commentsbutton.get(0).click();

if(commentsbutton.size() > 0) {
    for (WebElement element : commentsbutton) {
        element.click(); //click on button if found
        Thread.sleep(5000); //pause for 5 seconds
    }
}           
Stanislav
  • 27,441
  • 9
  • 87
  • 82