Selenium:如何忽略2个以上的异常

时间:2016-05-22 11:36:17

标签: scala selenium

所以我有这个通用函数,等待ExpectedCondition上的元素库:

def findMyElement(expectedCondition: (By) => ExpectedCondition[WebElement], by: By, timeOut: Long): WebElement = {

    createFluentWait(timeOut).until(expectedCondition(by))
    driver.findElement(by)
  }

 def createFluentWait(timeOut: Long): FluentWait[WebDriver] = {

    new FluentWait[WebDriver](driver)
      .withTimeout(timeOut, TimeUnit.SECONDS)
      .pollingEvery(1, TimeUnit.SECONDS)
      .ignoring(classOf[NoSuchElementException], (classOf[StaleElementReferenceException]))
  }

所以我的问题是如何在我的Exceptions函数中添加另一个createFluentWait,以便在超时之前避免Exception? (忽略只获得2)

2 个答案:

答案 0 :(得分:1)

您应该能够为createFluentWait功能添加另一个.ignoring调用。您想忽略哪些其他例外?

def createFluentWait(timeOut: Long): FluentWait[WebDriver] = {

    new FluentWait[WebDriver](driver)
        .withTimeout(timeOut, TimeUnit.SECONDS)
        .pollingEvery(1, TimeUnit.SECONDS)
        .ignoring(classOf[NoSuchElementException])
        .ignoring(classOf[StaleElementReferenceException])
        .ignoring(classOf[SomethingElseException])
}

答案 1 :(得分:0)

您可以连结另一个ignoring

def createFluentWait(timeOut: Long): FluentWait[WebDriver] = {

    new FluentWait[WebDriver](driver)
        .withTimeout(timeOut, TimeUnit.SECONDS)
        .pollingEvery(1, TimeUnit.SECONDS)
        .ignoring(classOf[NoSuchElementException, (classOf[StaleElementReferenceException]))
        .ignoring(classOf[NullPointerException, (classOf[StaleElementReferenceException]))
}