Depends on what you are doing. You can query the elements that are hidden behind the Notification and access their attributes without an issue. However, if you want to click on anything, that's supposed to emulate the end user experience, so it moves the cursor to the given position and tries to click whatever element happens to be there. Whether the Notification has focus or not makes no difference.
Now the good news is that you can close the Notification by clicking on it. The bad news is that webElement.click()
throws an exception if the click tries to land on the wrong element. So some workaround is needed beyond just clicking twice.
Here are a few potential options, all a bit clunky, but should do the trick:
- Query for notifications and close them before the click (can be called safely whether there are any Notifications or not):
private void closeAnyNotifications() {
for (WebElement notification : findElements(
By.className("v-Notification"))) {
notification.click();
}
}
- Surround the click with try-catch and if the error happens, only then close notifications and try the click again:
try {
webElement.click();
} catch (ElementClickInterceptedException e) {
closeAnyNotifications();
webElement.click();
}
- Don't use the click directly but through an action, and do another click if the first one didn't trigger whatever the click was supposed to trigger:
new Actions(getDriver()).moveToElement(webElement).click().perform();
- Do the try-catch trick but with the Action workaround (no need to move to element because the failed click already moved cursor to the correct location, only works if there can't be more than one Notification blocking the way):
try {
webElement.click();
} catch (ElementClickInterceptedException e) {
// close the notification
new Actions(getDriver()).click().perform();
webElement.click();
}