35. WebDriver Explicit Wait : How to wait till element visible or appear or present on page

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.

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[@name='fname']")));

package Others;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class webdriver_explicit {
                       
            @BeforeClass
            public void beforeClass() {
                        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");   
                        driver = new ChromeDriver();
                        driver.manage().window().maximize();
                        driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
                       
            }
            public WebDriver driver;
            @Test
            public void testIsDiplayed_Enabled() throws Exception {
                       
                        WebDriverWait wait = new WebDriverWait(driver, 15);
                        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='fname']")));
                        driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("Text box is visible now");
                        System.out.print("Text box fname is now visible");
           
            } 
            @AfterClass
            public void afterClass() throws Exception {
                        driver.quit();
            } 
}


3 comments:

  1. Great article! I'll be sharing it with my friends and colleagues.servicenow online training

    ReplyDelete
  2. Oracle SOA 12c training equips professionals with advanced skills in Oracle SOA Suite 12c for enterprise application integration. This oracle soa 12c training covers composite development, BPEL processes, Mediator, web services, and deployment. Learners gain practical experience through real-world implementation.

    ReplyDelete
  3. Excellent content! Choosing the Best MongoDB Online Course is a smart step for developers who want to improve their database management skills.best mongodb online course

    ReplyDelete