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