Selenium WebDriver : How to selenium wait until element is visible or appear or present on page

Sometimes you need selenium wait until element is visible when your targeted element is not visible on page when page is loading.  If you can put something like wait until element is visible selenium java test then test execution will wait until condition match. 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.

Selenium wait for element to be visible

If you have a scenario of selenium wait for element to be 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 selenium wait element visible on page. However, we can wait for element to be clickable using elementToBeClickable(By locator) method as described in THIS ARTICLE.

Syntax to wait until element is visible selenium java 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).

Selenium wait until element is visible Example

@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