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();
            } 
}


No comments:

Post a Comment