繁体   English   中英

为什么我的方法没有捕获“超时异常”并打印到控制台?

[英]Why dosnt my method catch my 'Timeout Exception' and print to console?

为什么我的方法没有捕获“超时异常”并打印到控制台?

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

在此处输入图片说明

this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();最有可能的超时异常从this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();抛出this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled(); ,而您的try-catch块不会将该行括起来

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

您的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。 记录异常也没有意义,因为无论如何它将被转储到日志中。 通常,您希望脚本在引发异常时停止,除非继续执行不会受到异常影响。 这在很大程度上取决于您的情况,因此您将必须决定是否继续进行最终决定,这将决定如何处理引发的异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM