【问题标题】:Alternative way to replace 'Thread.sleep(5000)'替换“Thread.sleep(5000)”的替代方法
【发布时间】:2016-01-13 09:08:38
【问题描述】:

是否有替代“Thread.sleep(5000)”的替代方法?目前,cmetsbutton 仅单击一次,即使 cmetsbutton 还有其他元素,也会转到回复按钮的代码?所以我认为这可能是因为 'Thread.sleep(5000)' 页面没有完成加载。

          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;
           }

【问题讨论】:

  • "目前,cmetsbutton 只点击一次,即使 cmetsbutton 有其他元素,也会转到回复按钮的代码?" 您需要使用循环。
  • if-else 语句不是循环了吗?
  • 不,这不是循环。

标签: java sleep


【解决方案1】:

您可以为此使用循环,但您必须了解,此循环将在执行期间使用进程资源。我认为,无论如何,Thread.sleep 在您的情况下都是首选...

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

但正如你所说的

cmets 按钮仅单击一次,然后将转到代码

看来,您需要在commentsbutton 集合的元素上添加额外的循环,以便在转到回复按钮之前单击您拥有的所有 cmets 按钮。现在你只点击一个按钮,你会得到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
    }
}           

【讨论】:

    猜你喜欢
    • 2015-07-15
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2014-11-03
    相关资源
    最近更新 更多