2017-09-20 27 views
0

我正在使用Try和catch捕獲彈出消息,當有任何無效的用戶名或密碼。 但是,代碼只有擁有正確的登錄信息的作品,當彈出窗口與錯誤,則拋出....錯誤截圖下面連接嘗試並彈出消息catch趕上

Code Screenshot

Error Screenshot

+0

其中是 '{' 爲如果catch塊內部條件?這是錯字嗎? – Kapil

+0

請閱讀爲什麼[代碼截圖是一個壞主意](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-orrors)。代碼粘貼並正確格式化。 – JeffC

回答

2

變線從

catch(NoSuchElementException e) 

的處理,以

catch(UnhandledAlertException e) 

它可以工作。

+0

非常感謝你這個解決方案的工作..... –

1

希望錯誤這將幫助你

driver.switchTo().alert().accept(); 

另外,如果你想利用警報文本添加DesiredCapabilities

DesiredCapabilities dc = new DesiredCapabilities(); 
    dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE); 
    d = new FirefoxDriver(dc); 

然後你就可以通過

try { 
    click(myButton); 
} catch (UnhandledAlertException f) { 
    try { 
     Alert alert = driver.switchTo().alert(); 
     String alertText = alert.getText(); 
     System.out.println("Alert data: " + alertText); 
     alert.accept(); 
    } catch (NoAlertPresentException e) { 
     e.printStackTrace(); 
    } 
} 
0

變化從

趕上(NoSuchElementException異常E)的線

趕上(UnhandledAlertException E)