Subbarao Gaddam
Subbarao Gaddam

Reputation: 677

Waiting the driver until an element to disappear

While Automating the search functionality, enter some search key word and click on Search button, the results need to load in grid. It takes few seconds time to load and displaying "Loading..." text like this in fallowing div.

<div id="loadmask-1027-msgTextEl" class="x-mask-msg-text">Loading...</div>

How could I wait until this message disappear.

Upvotes: 4

Views: 9322

Answers (5)

Mohammad Sommakia
Mohammad Sommakia

Reputation: 1833

this is what I used in kotlin and it worked:

wait?.until(ExpectedConditions.invisibilityOf(driver.findElement(By.id("progress_bar"))))

Upvotes: 0

ThomasK
ThomasK

Reputation: 1

Another approach in Java, without "wait"-methods of Selenium, if the current code-examples are not suitable solutions to you. I'm using WebDriver with JUnit:

public void waitForElementToDisappear() throws InterruptedException{
//try for 5 seconds
for(int i=0;i<=5;++i){
if(driver.findElements(By.xpath(<elementExpectToDisappear>)).isEmpty()){
//here your code, if element finally disappeared
break;
}else{
Thread.sleep(1000);
}
if(i==5)
fail("element not disappeared within 5 seconds");
}
}

Upvotes: 0

p3nGu1nZz
p3nGu1nZz

Reputation: 1715

This works with selenium 2.4.0 All of the other solution are bugged. The apply method can not return a boolean, it must return a WebElement. The API documentation is also incorrect. Here is the correct code.

final public static boolean waitForElToBeRemove(WebDriver driver, final By by) {
    try {
        driver.manage().timeouts()
                .implicitlyWait(0, TimeUnit.SECONDS);

        WebDriverWait wait = new WebDriverWait(UITestBase.driver,
                DEFAULT_TIMEOUT);

        boolean present = wait
                .ignoring(StaleElementReferenceException.class)
                .ignoring(NoSuchElementException.class)
                .until(ExpectedConditions.invisibilityOfElementLocated(by));

        return present;
    } catch (Exception e) {
        return false;
    } finally {
        driver.manage().timeouts()
                .implicitlyWait(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
    }
}

Upvotes: 1

Paul Harris
Paul Harris

Reputation: 5819

Webdriver has built in waiting functionality you just need to build in the condition to wait for.

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

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

you will need to replace the By.id("xx") with however you identify the element you are expecting to go.

Upvotes: 4

Vinay
Vinay

Reputation: 754

You can create a method where you will identify if the element is present. Put this logic in a loop and wait for a second before going to the next iteration. You can probably wait for 10 to 15 seconds before you break completely from the loop. If the element disappears then it will throw an exception. You can use try catch and within catch you can break the loop. This will ensure code actually waits for element to disappear or max 10 to 15 seconds.

Upvotes: 0

Related Questions