简体   繁体   中英

Selenium Webdriver “Expectedconditions.not” is not working as expected

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

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.

you are not waiting for the element to be visible in the first statement,ie,

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.

您可以尝试以下方法:

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(d => d.FindElement(By.Id("searchTextBox0")).Displayed)

If you'll be using this multiple times, create a method. Eg if you'll be waiting for other elements elsewhere.

public void waitForElementToBeVisible(String xpath) throws Throwable {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 15);
        wait.until(ExpectedConditions.or(
                ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath))
        ));
    }
    catch(Exception e) {
        System.out.println("Timeout exceeded");
        driver.close();
    }
}

You can then call this method multiple times. To call the one you're stuck with it would be

waitForElementToBeVisible("//div[contains(text(),'Loading...')]");

If all you need to do is wait for the page to load, you can execute a Javascript function to check page load has completed:

String val = "";
do {
    val = (String)((JavascriptExecutor)driver).executeScript("return document.readyState");
    // DO WHATEVER
} while (!"complete".equals(val));

When using findElement() you must use implicit wait before attempting to locate the element. Otherwise, NoSuchElementException could be thrown if the component is not loaded on the page:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Blocks NoSuchElementExceptions for 5 seconds when findElement() is called (set for the duration of driver's life.
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));

That strategy should be used judiciously as it will most likely affect the performance of your tests. Alternatively, you should use WebDriverWait (explicit wait):

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))); // Presence of component checks the existence of the element in the DOM which it will always be true
System.out.println("Testing visibility passed...");
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))); // Presence of component checks the existence of the element in the DOM which it will always be true
System.out.println("Testing invisibility passed...");

Be aware that on the last strategy, visibilityOfElementLocated returns a WebElement and visibilityOfElementLocated returns a Boolean . Therefore, you cannot chain the conditions by using .andThen(Function) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM