I am trying to automate a scenario where I am encountering a window modal dialog box. Please let me know how to automate this situation? I just want to know how to click the highlighted OK button on the popup appearing? Please suggest
Asked
Active
Viewed 927 times
4 Answers
1
Try this
driver.switchTo().alert().accept();

Mate Mrše
- 7,997
- 10
- 40
- 77
-
Tried this as well but hard luck. The control was not switching to the alert box so it wasn't able to click OK button – Prashant1992 Jan 10 '19 at 13:29
-
org.openqa.selenium.UnhandledAlertException: Modal dialog present: Please Enter a Search Criteria. – Prashant1992 Jan 16 '19 at 10:09
-
Please check this is the exception I am facing – Prashant1992 Jan 16 '19 at 10:09
-
1See if [this](https://stackoverflow.com/questions/26772793/org-openqa-selenium-unhandledalertexception-unexpected-alert-open) is of any help. – Mate Mrše Jan 16 '19 at 10:24
-
Just did the same as the above one Mate – Prashant1992 Jan 16 '19 at 10:58
1
You can also send keyboard events to press enter key as soon as pop up is active Pressing enter key is as equivalent as clicking OK button
Make use of Robot class in java
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER)

Ankit Agrawal
- 257
- 1
- 12
-
-
1try adding delay statements in between `Robot r = new Robot();` `r.delay(500);` `r.keyPress(KeyEvent.VK_ENTER);` `r.delay(500);` `r.keyRelease(KeyEvent.VK_ENTER)` `r.delay(500);` – Ankit Agrawal Jan 14 '19 at 08:19
0
You can try this by using JavascriptExecutor. It always works if we fail to find element using findelement method

Dish
- 117
- 8
0
I just found a way to handle this issue.
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
d = new FirefoxDriver(dc);
then implemented the alert code in try catch block
try
{
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert data: " + alertText);
alert.accept();
}
catch (UnhandledAlertException e)
{
e.printStackTrace();
}

Prashant1992
- 31
- 5