Implicitly Wait in Selenium
Implicitly wait is one of the ways to request selenium not throw any exception until provided time. The default wait time of the selenium is 500 milliseconds, implicitly wait overrides the default wait time of the selenium.
If the element is found before implicitly wait time, selenium moves to the next commands in the program without waiting to complete the implicitly wait time; this wait is also called dynamic wait.
Implicit wait tries to find the element in the first go if the element is not present implicit wait tries to find the element after 500ms of first polling, if the element is not available on the second time also then implicit wait tries the third time after 500 ms of the second try and it goes on till the time reaches the 30 seconds.
If the driver still does not find the element, then it throws an exception. Implicit wait does the same for all the elements in your program, so you just have to set it once.
WebDriver driver = new ChromeDriver();
// set implicit wait tme as 30 Seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Ex:
public class MultiValueDropdownSelectByValue {
public static void main(String[] args) throws Exception {
// set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
// open firefox
WebDriver driver = new FirefoxDriver();
// set implicitly wait time for 1 Minute
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
// open webpage
driver.get("https://chercher.tech/practice/practice-dropdowns");
// find the dropdown using xpath
WebElement dropdownElement = driver.findElement(By.xpath("//select[@id='second']"));
// create object for Select class
Select dropdown = new Select(dropdownElement);
// select options from the dropdown options which have value as 'donut', 'burger'
dropdown.selectByValue("donut");
}
}
Timeunits with Implicit Wait
- TimeUnit.NANOSECONDS : represents nano seconds (10pow-9 of a second is nanosecond)
- TimeUnit.MICROSECONDS : represents microseconds (10pow-6 of a second is microsecond)
- TimeUnit.MILLISECONDS : represents mill seconds (10pow-3 of a second is microsecond, i.e. 1000 milliseconds is a second)
- TimeUnit.SECONDS : represent a second
- TimeUnit.MINUTES : represent minutes, a minute is 60 seconds
- TimeUnit.HOURS : represent the hour, an hour is 60 minutes
- TimeUnit.DAYS : represents the day, a day is 24 hours