I use the Page-Object-Model, so I have referenced elements (by default at least), and then I have a conditional "popup" element that displays some information, with a covering div in the background. So, similar effective situation to the OP.
The solution I use related to your issue is to check for the relevant popup element. If it exists, perform the close, then execute the wait (using invisibilityOf()
) on the same relevant element (which is part of the popup), regardless of the next operation. This then ensures the next operation does not get blocked. It works in my case because the visibility of the element inherits the visibility of the popup structure (W3C).
Here are the relevant parts for this setup:
@FindBy(how = How.XPATH, using = "//*[@id='content_panel']/div[2]/div[1]/span")
private WebElement onlineRegistrationPopupClose;
public void CloseOnlineRegistrationPopup() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 5);
// try and close this, then wait a moment for the element to be invisible
if (onlineRegistrationPopupClose.isDisplayed()) {
onlineRegistrationPopupClose.click();
wait.until(ExpectedConditions.invisibilityOf(onlineRegistrationPopupClose));
}
}