How to apply wait in webdriver till element becomes invisible or hidden

As you know, WebDriver is software web application regression testing tool and you will face many problems during your webdriver test case creation and execution. You must knows alternate solution if one not works for your software automation test scenario. For example, you have used IMPLICIT WAIT in your software automation test case but if it is not sufficient to handle your condition of waiting till element becomes invisible from page then you can use it's alternative way of using EXPLICIT WAIT in your test case.

In my PREVIOUS POST, I have describe use of visibilityOfElementLocated(By locator) method with explicit wait to wait till element becomes visible (not hidden) on software web application page. Now let we look at opposite side. If you wants webdriver to wait till element becomes invisible or hidden from page then we can use invisibilityOfElementLocated(By locator) method with wait condition.

Syntax for wait till element invisible from page is as bellow.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//input[@id='text4']")));

In above syntax, you can change webdriver wait time from 15 to 20, 30 or more as per your requirement. Also you can use any OTHER ELEMENT LOCATING METHODS except By.xpath. Practical example of webdriver wait till element becomes invisible is as bellow.

Copy bellow given @Test method part of wait till element invisible and replace it with the @Test method part of example given on THIS PAGE.(Note : @Test method is marked with pink color in that linked page).

@Test
 public void test () throws InterruptedException, IOException 
 { 
  //Wait for element invisible
  WebDriverWait wait = new WebDriverWait(driver, 15);
  wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//input[@id='text4']")));

  System.out.print("Text box text4 is now invisible");      
 }

You can VIEW EXAMPLE OF "waitForElementNotPresent" IN SELENIUM IDE if you are using selenium IDE.

5 comments:

  1. Hi what happens if the element we are looking for invisibility is not loaded in the page?
    For Ex. i am checking for text4 invisibility but that input box is not loaded/present in the page then it will wait till webdriver wait is completed or it will throw any error ????

    ReplyDelete
    Replies
    1. will give an error, regarding element is not visible

      Delete
  2. According to above explanation it waits what time we specify it waits up to that time

    ReplyDelete
  3. show,how the output comes with diagram.
    not only coding...

    ReplyDelete