如何使我的方法使用 Wait.Until 忽略 selenium webdriver 中的抛出异常

Ali*_*adi 0 java selenium-webdriver

我有一个方法是

private boolean findElements(
                                  String xpath,
                                  int timeOut ) {

        WebDriverWait wait = new WebDriverWait( driver, timeOut );

        try {
            if( wait.until( ExpectedConditions.visibilityOfElementLocated( By.xpath( xpath ) ) ) != null ) {
                return true;
            } else {
                return false;
            }
        } catch( NoSuchElementException e ) {
            e.printStackTrace();
            return false;
        }

    }
Run Code Online (Sandbox Code Playgroud)

它在找到元素时返回 true,但在未找到元素时抛出异常,我如何使方法返回 false 而不是抛出异常,或者是否有更好的布尔值方法或方法可以为我完成这项工作.

亲切的问候

Ali*_*adi 5

李在轩回答了这个问题

  { 
    wait.until( ... ); return true;
  }
 catch(TimeoutException ex)
  { return false; } //might work if the wait.until( ... ) throws a TimeoutException
Run Code Online (Sandbox Code Playgroud)

报告说没有

  • 你会想要捕捉一个 WebDriverTimeoutException (4认同)