So, I have an element which is hidden under an alert. Alert stays for 10 secs and the user can click the element after that. Here is my code to deal with this situation:
WebElement create = driver.findElement(By.cssSelector("div.action_menu_trigger"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(create));
create.click();
but I get this exception as soon as WebDriver reaches here, seems like Selenium doesn't care about wait method:
org.openqa.selenium.ElementClickInterceptedException:
Element <div class="action_menu_trigger"> is not clickable at point (1710.224952697754,140) because another element <div class="noty_body"> obscures it
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z'
I have tried by using Thread.sleep(10000) and it works fine but I don't want to use sleep.
The problem here is that the element under the alert IS clickable as far as Selenium knows. It is visible and enabled so it should be clickable. Your code waits for the element to be clickable (assuming it will wait for the alert to disappear) but Selenium already thinks the element is clickable so it attempts a click immediately resulting in the error message because the alert is still up and blocks the click.
The way around this is to wait for the alert to appear then disappear, wait for the element to be clickable, and click it. I don't know that I have all the locators but the code below should get you pointed in the right direction.
// define locators for use later
// this also makes maintenance easier because locators are in one place, see Page Object Model
By alertLocator = By.cssSelector("div.noty_body");
By createLocator = By.cssSelector("div.action_menu_trigger");
// do something that triggers the alert
// wait for the alert to appear and then disappear
WebDriverWait shortWait = new WebDriverWait(driver, 3);
WebDriverWait longWait = new WebDriverWait(driver, 30);
shortWait.until(ExpectedConditions.visibilityOfElementLocated(alertLocator));
longWait.until(ExpectedConditions.invisibilityOfElementLocated(alertLocator));
// now we wait for the desired element to be clickable and click it
shortWait.until(ExpectedConditions.elementToBeClickable(createLocator)).click();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With