Gbru
Gbru

Reputation: 1087

Correct wait to wait for an element to disappear?

Correct wait to wait for an element to disappear?

I have an ajax loader which loads after for example clicking on a button, is my method correct inorder to wait for a particular load bar which takes the full width and height of a screen to disappear?

    public void waitUntilAjaxLoaderDisapears() {
    // Wait up to 2minutes for the element to disappear
    WebDriverWait ajaxWait = new WebDriverWait(this.driver, 60);
    ajaxWait.pollingEvery(100, TimeUnit.SECONDS);
    try {
        //tempWait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".modal-body")));
        ajaxWait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[contains(@class, 'ajax_loader')]")));
    } catch (UnhandledAlertException e) {
         Alert alert = driver.switchTo().alert();
         alert.accept();
    }catch (NoAlertPresentException e) {
        //
    }catch (StaleElementReferenceException e) {
        // do nothing
    } catch (NullPointerException e) {
        // do nothing
    } catch (Exception e) {
        // do nothing
    }
}

Upvotes: 0

Views: 722

Answers (1)

santhosh kumar
santhosh kumar

Reputation: 2019

I think i got the issue. The polling time should be less than the overall wait time. So it can be,

 WebDriverWait ajaxWait = new WebDriverWait(this.driver, 60);
 ajaxWait.pollingEvery(**5**, TimeUnit.SECONDS);

Instead of

WebDriverWait ajaxWait = new WebDriverWait(this.driver, 60);
ajaxWait.pollingEvery(100, TimeUnit.SECONDS);

Hope this helps you. Thanks.

Upvotes: 1

Related Questions