Problem
I am writing automated tests for my company's website using Java and Selenium. Right now I am writing tests that involve clicking on links, and verifying that the link leads to the correct place. We have a newsletter popup (from BounceExchange) that appears at very unpredictable times, and it's causing ElementClickInterceptedExceptions. This is the exception message:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <li id="menu-item-78096" ... is not clickable at point (x, x). Other element would receive click <div class="bx-slab">...</div>
What I've Tried
Clicking with JavaScript
I have been clicking WebElements with driver.findElement(By...).click()
, which I've read is the best way to click things when testing UI. I have tried doing a JavaScript click like this
but that hasn't worked, and the page just hangs with the popup on screen.
Closing the Popup
This is the area where I have had the most success, but it still doesn't fully work. This is my click method:
public void click(By elementBy) {
By bounceExchange = By.className("bx-slab");
By bounceExchangeClose = By.className("bx-close");
//close bouncex if its open
if(elementExists(bounceExchange)) {
WebElement bounceX = driver.findElement(bounceExchange);
if(bounceX.isDisplayed()) {
System.out.println("Closing bounce exchange");
try {
driver.findElement(bounceExchangeClose).click();
}
catch(Exception e) {
//ignore
}
}
}
driver.findElement(elementBy).click();
}
From looking at the HTML, I know that the close button on the popup has a class "bx-close". This method sometimes successfully closes the popup, but then link that is being tested is never clicked, and the test fails.
Pressing Escape
I have learned that pressing the escape key while the popup is on screen makes it go away.
I have tried two ways to do this
1. Using Actions
Actions actions = new Actions(driver);
actions.keyDown(Keys.ESCAPE).build().perform();
actions.keyUp(Keys.ESCAPE).build().perform();
But this way was giving me an illegal argument error since the escape key is not a modifier key.
And I have tried WebDriver's sendKeys
:
driver.findElement(By.tagName("html")).sendKeys(Keys.Escape);
This approach also did not work for me.
HTML of Popup
<div class="bx-slab">
<div class="bx-align">
<div class="bx-creative bx-creative-874700" id="bx-creative-874700">
<div class="bx-wrap">
<a id="bx-close-inside-874700" class="bx-close bx-close-link bx-close-inside" href="javascript:void(0)" data-click="close">
<svg class="bx-close-xsvg" viewBox="240 240 20 20">..</svg>
</a>
...
</div>
So my question is, what is a reliable way to close this unpredictable popup so I can stop getting ElementClickInterceptedExceptions?