2017-09-20 32 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)