To wait means having the task to stop execution for a certain amount of time. After the the time is elapsed the execution continues with he next steps. Selenium API provides two flavours of waits: Explicit Wait and Implicit Wait.
Explicit Wait is the code to stop execution for a certain time until an expected condition is met. The expected condition may be the presence of any element of the visibility of the element. As soon as the expected condition is evaluated to be true the execution resumes. For instance, if an explicit wait of 10 seconds is set and we want to stop the execution until an element is present on the page, the WebDriver will on every 500 miliseconds check to see if the element is present on the page or not. If the element is found(i.e., the condition becomes true before 10 seconds), the execution will resume. In case the condition is not met and the time(10 seconds in this case) has elapsed the TimeoutException is thrown. There are few convenience method provided by the API to implement the expected conditions.
WebDriver driver = new FirefoxDriver(); driver.get("http://www.example.com"); WebElement element = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("some_element_id"))); //Element is present
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("some_element_id"))); //Element to be displayed and enabled to be clicked
Another way to implement the explicit wait is to use the Thread.sleep() (not recommended) method, which will cause the current Java Thread to stop execution for a certain amount of time. Note that this is not a preferred way to implement explicit wait as it can cause the task to run into deadlock and other threading related issues.
Implicit wait is a way to tell the driver to look into the DOM for an element for certain amount of time if the element is not available immediately. By default the time for the implicit wait is 0 seconds. This implicit time can be changed and once changed it will be used till the life of WebDriver object. Of course this can again changed later the same way it was set earlier. Implicit wait especially usefull when the elements are created dynamically on the page, a modal box, for example. Below is an example of setting the implicit time wait.
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.example.com"); WebElement element = driver.findElement(By.id("some_element_id"));
In the above example please note that the implicitlyWait() method takes the two arguments the first is the amount of time and the second is the unit of time. Hence the above example will set the implicit wait to 10 seconds. This way the findElement() will not return immediately if the element is not found, instead it will wait for 10 seconds for the element to be present and in between the waiting it will keep polling (after every 500 milliseconds) the DOM for the element. As soon as the element is present it will be returned.