为什么dosnt我的方法会抓住我的'Timeout Exception'并打印到控制台?
public void clickDrivingExperienceButton() throws Exception {
boolean test = this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();
try {
if (test == true) {
link_DrivingExperiences.click();
}
System.out.println("Successfully clicked on the driving experience button, using locator: " + "<" + link_DrivingExperiences.toString() + ">");
}catch (TimeoutException e) {
System.out.println("WHY DONT I PRINT ANYTHING??????" + e.getMessage());
}catch (Exception e) {
System.out.println("Unable to click on the Driving Experience Button, Exception: " + e.getMessage());
} finally {
// final code here
}
}
答案 0 :(得分:0)
很可能超时异常抛出this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();
,并且你的try-catch块没有包含该行
答案 1 :(得分:0)
把this.wait.until放在try块中。
异常消息已经告诉它在等待元素可点击时发生异常。
public void clickDrivingExperienceButton() throws Exception {
try {
boolean test = this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();
if (test == true) {
link_DrivingExperiences.click();
}
System.out.println("Successfully clicked on the driving experience button, using locator: " + "<" + link_DrivingExperiences.toString() + ">");
}catch (TimeoutException e) {
System.out.println("WHY DONT I PRINT ANYTHING??????" + e.getMessage());
}catch (Exception e) {
System.out.println("Unable to click on the Driving Experience Button, Exception: " + e.getMessage());
} finally {
// final code here
}
}
答案 2 :(得分:0)
您的try-catch
没有抓住异常,因为异常来自第二行(wait.until
)且不在try-catch
内。
您在我的另一个问题https://stackoverflow.com/a/42120129/2386774中遇到了许多相同的问题,我建议您也修复此代码。
基本上应该是以下
public void clickDrivingExperienceButton() throws Exception
{
this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).click();
}
您不应该像登录一样记录日志。如果成功单击该元素,则脚本将继续。记录只会阻塞你的日志,IMO。记录异常也没有意义,因为无论如何它都会被转储到日志中。通常,您希望脚本在抛出异常时停止,除非继续不受其影响。这在很大程度上取决于您的场景,因此您必须做出是否继续的最终决定,这将决定您如何处理抛出的异常。