【问题标题】:What is the difference between explicit wait and fluent wait in webdriver?webdriver中的显式等待和流畅等待有什么区别?
【发布时间】:2015-03-04 11:24:36
【问题描述】:

下面是我的代码

public boolean waitForElementToBeClickable(String xpathValue){

        Boolean flag = false;
        WebElement myDynamicElement = (new WebDriverWait(Base.base.getDriver(), 30))
                  .until(ExpectedConditions.elementToBeClickable(By.xpath(xpathValue)));
        flag = true;
        return flag;

    }

这会等待元素出现 30 秒,如果元素在 30 秒之前准备好,则执行下一个命令。

但是当我使用下面的代码进行流畅的等待时

public Boolean waitForElementToAppear(final WebElement e){

         Wait<WebElement> wait = new FluentWait<WebElement>(e)
                 .withTimeout(30, TimeUnit.SECONDS)              
                 .pollingEvery(2, TimeUnit.SECONDS)
                 ;

         Boolean foo = wait.until(new Function<WebElement, Boolean>() {
             public Boolean apply( WebElement element) {

               return element.isDisplayed();
             }
           });
        return foo;
         }

它说 No-such Element 异常,它甚至不等待 30 秒,它立即抛出异常。

谁能告诉它有什么问题?

【问题讨论】:

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

通常,当您使用FluentWait 时,您至少必须在等待期间忽略NoSuchElementException

像这样:

Wait<WebElement> wait = new FluentWait<WebElement>(e)
             .withTimeout(30, TimeUnit.SECONDS)              
             .pollingEvery(2, TimeUnit.SECONDS)
             .ignoring(NoSuchElementException.class);

【讨论】:

  • 如果我使用忽略测试通过但实际上它不应该因为元素没有被点击。
  • 我不知道你测试代码,你能分享一下吗?无论如何,你的代码正在等待元素可见“return element.isDisplayed();”不可点击。如果你想要一个等价的等待,你应该修改你的条件。
猜你喜欢
  • 2017-04-19
  • 2014-05-04
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 2018-03-04
  • 2015-04-23
相关资源
最近更新 更多