0

I have this block UI overlay, that sometimes when I try to click on a item(button or anything) it's superimposes or obscures this action, so whenever I try to click I received this ElementClickInterceptedException. Element Obscured.

I want to make a if, that IF this error is received wait until this BLOCK UI class disappears, then try to click it again.

But with this logic, The error is still received and the framework fails to continue, throws the error and pass to the next TestCase

if (driver.FindElement(By.ClassName("block-ui-overlay")).Displayed)
{
    WebDriverWait waitForElement = new WebDriverWait(driver, TimeSpan.FromSeconds(5000));
    waitForElement.Until(ExpectedConditions.InvisibilityOfElementLocated(By.ClassName("blockUI blockOverlay")));
}
managedg.MAN_DistGroups.Click();

Fluentwait:

public static void BlockUIWait(IWebDriver driver , string selector)
{
      DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver);
      fluentWait.Timeout = TimeSpan.FromSeconds(5);
      fluentWait.PollingInterval = TimeSpan.FromMilliseconds(150);
      fluentWait.IgnoreExceptionTypes(typeof(ElementClickInterceptedException));
      fluentWait.Until(ExpectedConditions.ElementToBeClickable(driver.FindElement(By.CssSelector(selector))));

Result message:

OpenQA.Selenium.ElementClickInterceptedException : Element <div class="ng-scope"> is not clickable at point (404,613) because another element <div class="blockUI blockOverlay"> obscures it

bsullit
  • 101
  • 3
  • 11
  • You realize your first locator isn't the same as the second? I don't think your code is getting inside the `if` because if it was, the second locator would throw an error because `"blockUI blockOverlay"` is two class names, not one. Put a breakpoint inside the `if` and see if you are getting there. – JeffC Sep 04 '19 at 13:14
  • Does the block UI always pop up? – JeffC Sep 04 '19 at 13:14
  • Yes it alway come in this action. As for the locators, Class name does not accept spaces and when I use CSS Selectors I receive the same message. – bsullit Sep 04 '19 at 13:26

2 Answers2

3

You can use FluentWait for this. In Fluent Wait, you can wait for a condition and ignore a specific exception which occurs during that waiting period

   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(ElementClickInterceptedException.class);

   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });

Refer to https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html for more details

Following is the c# implementation

DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(webdriver);
            fluentWait.Timeout = TimeSpan.FromSeconds(5);
            fluentWait.PollingInterval = TimeSpan.FromMilliseconds(250);
            fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
            IWebElement searchResult = fluentWait.Until(x => x.FindElement(By.Id("search_result")));
work_ishaan
  • 354
  • 1
  • 8
0

Since the UI always comes up, you can wait for it to appear and then wait for it to disappear. Assuming your locators are correct, this should work...

WebDriverWait waitForElement = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); // changed from 5000 because this is seconds and not milliseconds
By blockUI = By.CssSelector(".blockUI.blockOverlay");
waitForElement.Until(ExpectedConditions.VisibilityOfElementLocated(blockUI));
waitForElement.Until(ExpectedConditions.InvisibilityOfElementLocated(blockUI));
managedg.MAN_DistGroups.Click();

My assumption is that your locators should be the same but I can't see the page so I can't test it. Given the error message you provided, I think that the second locator should also be used in the first wait.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • make sense, but now it fails in the line where you wait for the visibility of this element... Error: OpenQA.Selenium.WebDriverTimeoutException : Timed out after 5 secondsResult message: – bsullit Sep 04 '19 at 13:42
  • Try the updated locator/code – JeffC Sep 04 '19 at 13:58