30
Jul

Generic function to wait for an element to be visible

Following generic code can be used to wait for webelement to be visible on page:

public static WebElement waitForElementToBeVisibleAfterRefresh(WebDriver driver, By locator) {
WebElement webElement;
try {
webElement = new WebDriverWait(driver, 10, 20).until(ExpectedConditions.visibilityOfElementLocated(locator));
}catch (Exception e) {
System.out.println(“Element has not been visible, searched by element”+e);
return null;
}

return webElement;
}

In order to use it with your frameworks:

WebDriver driver;
driver = new FirefoxDriver();
driver.get(“https://www.google.co.in”);
By addItem = By.name(“q”);
WebElement element = waitForElementToBeVisibleAfterRefresh(driver, addItem);
element.sendKeys(“Hello google”);