I am not sure if I know exactly what your problem is, since you ask about checking if a pop-up error is shown, but then you seem to be testing for a NoSuchElementException, which shouldn't cause a pop-up in the first place (since it's an error in the Java program and not the browser). If you really need to test for pop-up warnings, this worked for me:
private Alert getAlert() {
return ExpectedConditions.alertIsPresent().apply(driver);
}
This method returns the alert, and "driver" here refers to the implementation of the WebDriver interface in the same way that you're doing in your examples.
You can use the following method to see if an alert has been raised:
private boolean isAlertPresent() {
return ExpectedConditions.alertIsPresent().apply(driver) != null;
}
The below method, when passed an Alert (such as from the first method shown above), will return the alert's message:
private String getAlertText(Alert alert) {
return alert.getText();
}
However, sometimes merely ordering Selenium to click on an object that it doesn't think exists will cause it to raise an exception before the alert can be logged. If you face that problem, then the following may seem like a messy solution but it has worked for me:
private void clickAndCheckAlerts() {
try {
// (put code for clicking a component here)
// Optionally you may want make the thread sleep for a little while
// since alerts may not pop up immediately
Thread.sleep(SLEEP_TIME);
} catch (UnhandledAlertException e) {
// Make the following method check for alerts and log their text
checkForAlertsAndLogAlertText();
} catch (Exception e) {
e.printStackTrace();
}
}
This probably isn't the most polished or proper way to handle this situation, so if anyone knows a better way please inform us.