On my page, I have alerts that display sometimes. (these are actually notifications in Salesforce) These alerts break my scripts since my scripts can't find the elements behind the alerts. I'd like to check for the alerts and if they exist, dismiss them. If they don't exist, then move on to the next step.
Secondary problem is that there may be more than one of these alerts. So it may have dismiss anywhere from 1 to 6 or more alerts.
I've added this code to my test script and it works if there is ONE alert. Obviously my script fails if there is more than one alert or if there are zero alerts.
driver.findElement(By.xpath("//button[contains(@title,'Dismiss notification')]")).click();
I'm still learning java, so please be gentle. ;) But I'd love to put this into a method so it can look for those buttons, click if they exist, keep looking for more until it finds none, then move on. I just have no idea how to do it.
I'm using TestNG too, I know that makes a difference in what's allowable and what's not.
Thank you!