There are multiple ways of achieving waits with Selenium.
Thread.Sleep
Thread.Sleep is a static wait and it is not a good way to use in scripts as it is sleep without condition.
Thread.Sleep(2000); // this will wait for 2000 milliseconds
Explicit Waits
An explicit wait waits for a certain condition to occur before proceeding. For example it can be used when we want to check the visibility of an element before firing actions in to it.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement DynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("DynamicElement")));
Implicit Wait
Implicit wait is used in cases where the WebDriver cannot locate an object immediately because of its unavailability. The WebDriver will wait for a specified implicit wait time and it will not try to find the element again during the specified time period.
Once the specified time limit is crossed, the webDriver will try to search the element once again for one last time. Upon success, it proceeds with the execution; upon failure, it throws exception.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.com");
WebElement DynamicElement = driver.findElement(By.id("DynamicElement"));
Fluent Wait
A FluentWait instance defines the maximum amount of time to wait for a condition to take place, as well as the frequency with which to check the existence of the object condition.
Wait wait = new FluentWait(driver).withTimeout(60, SECONDS).pollingEvery(10, SECONDS) .ignoring(NoSuchElementException.class);
WebElement dynamicelement = wait.until(new Function<webdriver,webElement>(){
public WebElement apply(WebDriver driver){
return driver.findElement(By.id("dynamicelement"));
}
});
You can use whatever method suites your situation.