Ankit
Ankit

Reputation: 199

Webdriver: ExpectedConditions.invisibilityOf is not working. Always throws an exception

I'm using ExpectedConditions.invisibilityOf to check the invisibility of one of the element, but every time it throws the timeout exception for :

wait.until(ExpectedConditions.invisibilityOf(elementTobeInvisible));

Error Message:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for invisibility of Proxy element for: DefaultElementLocator 'By.xpath: //button[text()='button text']' (tried for 30 second(s) with 500 MILLISECONDS interval)

I've checked visibility of same element using element.isDisplayed() it returns 'false' correctly.

Recently I upgraded selenium from 2.53.0

Upvotes: 2

Views: 6713

Answers (2)

Ali Azam
Ali Azam

Reputation: 2115

You can try using the below line of code instead of that -

wait.until(ExpectedConditions.invisibilityOfElementLocated(elementTobeInvisible));

Or you can simply call the method like this -

//Wait until Invisibility of element is completed
public void waitForInvisibility(By byElement){
try{
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.invisibilityOfElementLocated(byElement));
//May apply thread sleep for 1 or 2 seconds
}catch(Exception e){}}

Before call this method, declare the param By element like below -

By byElement = By.cssSelector("Use the element's css selector here");

//or

By byElement = By.xpath("Use the element's XPath here");

Upvotes: 0

Ankit
Ankit

Reputation: 199

Investigated the issue, looks like wrong implementation:

For version 2.53.0 and 3.7.0, there is implementation difference for invisibility method.

2.53 Implementation:

public static ExpectedCondition<Boolean> invisibilityOfAllElements(final List<WebElement> elements) {
    return new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver webDriver) {
            Iterator var2 = elements.iterator();

            while(var2.hasNext()) {
                WebElement element = (WebElement)var2.next();

                try {
                    if (element.isDisplayed()) {
                        return false;
                    }
                } catch (Exception var5) {
                    ;
                }
            }

            return true;
        }

        public String toString() {
            return "invisibility of all elements " + elements;
        }
    };
}

Observation: Look at line "catch (Exception var5) ". Here we are catching everything as an Exception


for 3.7.1 implementation

public static ExpectedCondition<Boolean> invisibilityOfAllElements(final List<WebElement> elements) {
    return new ExpectedCondition() {
        public Boolean apply(WebDriver webDriver) {
            return Boolean.valueOf(elements.stream().allMatch((x$0) -> {
                return ExpectedConditions.isInvisible(x$0);
            }));
        }

        public String toString() {
            return "invisibility of all elements " + elements;
        }
    };
}


private static boolean isInvisible(WebElement element) {
    try {
        return !element.isDisplayed();
    } catch (StaleElementReferenceException var2) {
        return true;
    }
}

Observation: We are only catching StaleElementReferenceException, hence while using the method throws TimeoutException which is not catched

Upvotes: 2

Related Questions