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.
WebDriver driver = new ChromeDriver();
// set implicit wait tme as 30 Seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
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