2017-02-09 28 views
0

爲什麼dosnt我的方法趕上我'超時異常'並打印到控制檯?爲什麼dosnt我的方法趕上我'超時異常'並打印到控制檯?

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

enter image description here

回答

0

最有可能超時異常拋出的this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();,和你的try-catch塊不封閉該行

+0

如果你發佈完整的堆棧跟蹤,它會更清晰地發生線路異常 –

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

如果我這樣做是由於某種原因設置的時間可以說10秒dosnt工作 – Gbru

+0

交叉檢查你的代碼。把聲明放在裏面不應該引起任何問題。現在,例外情況將被捕獲並打印。 –

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

你不應該真的需要爲你記錄到日誌之多。如果元素被成功點擊,腳本將會繼續。記錄這隻會阻塞你的日誌,海事組織。無論如何,記錄異常也沒有意義,因爲它將被轉儲到日誌中。通常,當拋出異常時,您希望腳本停止,除非繼續操作不受其影響。這在很大程度上取決於你的情況,所以你將不得不做出是否繼續進行的最終決定,這將決定你如何處理拋出的異常。

+0

再次感謝您在以下列出的方法中,我將如何更改方法,使其即使在單擊該方法時仍然不成功? – Gbru

+0

在'wait.until()'周圍拋出'try-catch'並捕獲'TimeoutException'。 – JeffC

相關問題