SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Selenium WebDriver : How to wait till element visible or appear or present on page

In any software web application's webdriver test case, you can easily wait for presence of element using IMPLICIT WAIT. If you have used implicit wait in your test case then you do not require to write any extra conditional statement to wait for element visibility. But in some cases, if any element is taking too much time to be visible on software web page then you can use EXPLICIT WAIT condition in your test case.

If you have a scenario to wait till element visible on software web page then selenium webdriver/Selenium 2 has its own method named visibilityOfElementLocated(By locator) to check the visibility of element on software web page. We can use this method with explicit webdriver wait condition to wait till element visible of present on page. However, we can wait for element to be clickable using elementToBeClickable(By locator) method as described in THIS ARTICLE.

Syntax to wait till element visible on page is as bellow. It will wait max 15 seconds for element. As soon as element visible on page, webdriver will go for executing next statement.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='text3']")));

Here is practical example of wait for element visible. Execute it once and then use it for your software web application test scenario.

Copy bellow given @Test method part of wait for visibility of element located example 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 
 {   
  //To wait for element visible
  WebDriverWait wait = new WebDriverWait(driver, 15);
  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='text3']")));
  
  driver.findElement(By.xpath("//input[@id='text3']")).sendKeys("Text box is visible now");
  System.out.print("Text box text3 is now visible");
    
 }

No comments:

Post a Comment