Getting Exception in alert

I am getting below exception while handling alert in my test:

Exception in thread “main” org.openqa.selenium.UnhandledAlertException: Unexpected modal dialog

I am using below code:

try {

driver.manage().timeouts()
.implicitlyWait(280, TimeUnit.SECONDS);
Alert alt = driver.switchTo().alert();

actualBoxtitle = alt.getText(); // get content of the Alter

alt.accept();
System.out.println(actualBoxtitle); // Message

if (actualBoxtitle.contains(“User or Password is not valid”)) { // Compare

System.out.println(“Test case SS[” + i + “]: Failed”);
}
} catch (NoAlertPresentException Ex) {

System.out.println(“Test case SS[” + i + “]: Passed”);
}

Please help me out.

Add Comment
3 Answer(s)

Rohit,

The only way to get the solution is to see the more of the stack trace of the exception. Also, instead of accept try to dismiss the alert. There might be a possibility that alert as no default accept method.

Also, it seems that exception is not coming in this code, it might be coming after the code. It would be helpful if you point out on which line the exception came.

Please be as elaborate as possible in explaing the question, just by looking at code its hard to find the cause of exception.

Virender

Virender Singh Professor Answered on April 24, 2015.
Add Comment

Hi Virender,

Thanks for your help. Actually I have debugged the code and found exception  coming at below line:

“Alert alt = driver.switchTo().alert();”

If I will use explicit wait instead of implicit wait. Will it work?
Please suggest.

Thanks,

rohitsri1339 Default Answered on April 24, 2015.
Add Comment

Hey Rohit,

It becomes very specific now, could you share with us the URL?

Can you tell me what type of Alert you are getting, compare it with top three buttons here

http://www.toolsqa.com/automation-practice-switch-windows/

virender

Virender Singh Professor Answered on April 24, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.