How to make the WebDriverWait.Until() method to NOT whitelist the NoSuchElementException



  • I am using the WebDriverWait.Until() method in my script to wait for an element to disappear from the UI.

    For this, there is the ExpectedConditions class and I could have used its InvisibilityOfElementLocated method, but it is now deprecated.

    In my implementation of the WebDriverWait.Until() method, I am passing a Func delegate which has the wait logic defined inside it.

    What I observe is that the exceptions, like NoSuchElementException is ignored(which I assume is due to the default whitelisted exceptions).

    But I have not used the IgnoreExceptionTypes method to explicitly whitelist any exception.

    I want the flow to break out of the Until method's check(which kind of keeps looping until one of the below mentioned event occurs) once an exception like NoSuchElementException is encountered.

    enter image description here

    Kindly suggest.



  • If you are providing automation testing services then you need to play with the waits for application to application. Put the code in try catch block and wait for the element only if it present:

    if (isElementDisplayed(By.xpath("abc"))) {
                waitForInvisibilityOfElement(webelement, 90);
            }
    

    public boolean isElementDisplayed(By by) {
    WebElement webElement = null;

        boolean displayed;
        try {
            webElement = this.getWebDriverFacade().getWebDriver().findElement(by);
            displayed = webElement.isDisplayed();
        } catch (StaleElementReferenceException var7) {
            try {
                webElement = this.getWebDriverFacade().getWebDriver().findElement(by);
                webElement.isDisplayed();
                displayed = true;
                logElementState(webElement, displayed, "exist");
            } catch (WebDriverException var6) {
                displayed = false;
                LOGGER.trace("[ELEMENT] {} is NOT {}", displayed, "exist");
            }
        } catch (WebDriverException var8) {
            displayed = false;
        }
    
        logElementState(webElement, displayed, "displayed");
        return displayed;
    }
    

    public void waitForInvisibilityOfElement(WebElement element, long timeoutInSeconds) {
    LOGGER.trace("[WAIT FOR] invisibility of element");

        try {
            this.getStandardWait().ignoring(NoSuchElementException.class).withTimeout(Duration.ofSeconds(timeoutInSeconds)).until(ExpectedConditions.invisibilityOf(element));
        } catch (NoSuchElementException var5) {
        }
    
    }
    




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2