Doesn’t this statement throws the NoSuchElementException when there is no Alert is present ?
I have handle the same as below my query is without having the exception thrown if alert is present then it should click otherwise without throwing the exception I should be able to handle the alert
try { driver.switchTo().alert().accept(); } catch (NoAlertPresentException e) { logger.warn("Alert is not present" + e.getMessage()); }