Selenium Webdriver "Expectedconditions.not" is not working as expected

21,785

Solution 1

When you want to wait for element to be not present, instead of presenceOfElementLocated use presenceOfAllElementsLocatedBy:

wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[contains(text(),'Loading...')]"))));

It will wait until there are no elements on the page that fit the locator.

Solution 2

you are not waiting for the element to be visible in the first statement,i.e,

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));

i think this is causing the NoSuchElementException...
you can try the following :

new WebDriverWait(driver,60).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]")));

new WebDriverWait(driver,60).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]")));

the above code will first wait for the visibility of the element,and then its invisibility.

Share:
21,785
Srivardhan
Author by

Srivardhan

Updated on August 02, 2022

Comments

  • Srivardhan
    Srivardhan over 1 year
    WebDriverWait wait = new WebDriverWait(driver, 60)
    
    WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));
    System.out.println("Test");
    wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))));
    System.out.println("Test");
    

    Trying to wait for the page loading to be completed. The first "test" is printed in the console and below exception is printed on exceuting the wait.until statement. Even after the loading screen is gone the wait.until is still waiting. Already tried Staleness of the element also and does not work, getting the same timeout exception. Once the loading is completed the element is no more available in the DOM