Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SeleniumWebDriver中创建使用Wait.Until ignore引发异常的my方法_Java_Selenium Webdriver - Fatal编程技术网

Java 如何在SeleniumWebDriver中创建使用Wait.Until ignore引发异常的my方法

Java 如何在SeleniumWebDriver中创建使用Wait.Until ignore引发异常的my方法,java,selenium-webdriver,Java,Selenium Webdriver,我有一个方法就是 private boolean findElements( String xpath, int timeOut ) { WebDriverWait wait = new WebDriverWait( driver, timeOut ); try { if( wait.until( Ex

我有一个方法就是

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

    }
当找到元素时返回true,但当找不到元素时抛出异常,我如何使返回false的方法代替异常抛出,或者有没有更好的布尔方法可以帮我完成这项工作


亲切问候

李在宪回答了这个问题

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

报道说没有

李在宪回答了这个问题

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

报告说不

我想你甚至不需要“else”块。。捕获异常,甚至不打印它。。。return false您是否尝试捕获TimeoutException而不是NosTouchElementException?是的,您是对的,我甚至不需要else,代码永远不会到达else参数,是的,我尝试了TimeoutException,问题是我编写的if条件永远不会返回null,因为它基本上是在找不到元素时抛出异常。我想知道是否有更好的方法来达到这个方法的目的。@AliHamadi我想
尝试{wait.until(…);return true;}catch(TimeoutException ex){return false;}
可能会工作,如果
wait.until(…)
抛出一个TimeoutException报告没有找到任何元素。非常感谢Jae!!!它起作用了,老实说,我不太擅长抛出异常错误以及它们的工作方式,我认为每个方法都会抛出一个我无法更改的特定异常错误,我认为NoTouchElementException是我唯一可以使用的方法,非常感谢JaeI,我想你甚至不需要“else”块。。捕获异常,甚至不打印它。。。return false您是否尝试捕获TimeoutException而不是NosTouchElementException?是的,您是对的,我甚至不需要else,代码永远不会到达else参数,是的,我尝试了TimeoutException,问题是我编写的if条件永远不会返回null,因为它基本上是在找不到元素时抛出异常。我想知道是否有更好的方法来达到这个方法的目的。@AliHamadi我想
尝试{wait.until(…);return true;}catch(TimeoutException ex){return false;}
可能会工作,如果
wait.until(…)
抛出一个TimeoutException报告没有找到任何元素。非常感谢Jae!!!它起作用了,老实说,我不太擅长抛出异常错误以及它们的工作方式,我认为每个方法都会抛出一个我无法更改的特定异常错误,我认为NoTouchElementException是我唯一可以使用的方法,非常感谢您,如果您想捕获WebDriverTimeoutException您想捕获WebDriverTimeoutException