Implicit Wait in Selenium WebDriver – Java (Synchronization)

  • Implicit wait tells the web driver to wait for a certain amount of time before throwing  an exception. In implicit wait, we give wait time globally and it will remain applicable to entire test script. WebDriver will wait for the element to load on the basis of time provided in wait condition. However, if web driver is unable to find an element in a given time, it will throw “ElementNotVisibleException“.
  • Web driver will keep on looking to DOM (Document Object Model) to check if the element is present or not. In case, web driver finds the web element before the time provided in implicit wait, it will exit the wait and starts the execution.

 Must Read: Handle ElementNotVisibleException in Selenium Webdriver

                     Explicit Wait in Selenium WebDriver

Syntax:

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);

Implicit wait method accepts two  parameters :

  • First parameter(Timeout) accepts time as an integer value.
  • Second parameter(TimeUnit.SECONDS) accepts time measurements.

Code for implicit wait:

  • In below code, we have given the implicit wait of 20 seconds. which means web driver will wait for 20 seconds at every step before throwing an exception.
  • However, web driver will keep on looking to DOM (Document Object Model) to check if the element is present or not. In case, web driver finds the web element before 20 seconds, it will exit the wait. Suppose web driver finds the web element in 5 seconds, then web driver will exit the implicit wait condition after 5 seconds and will starts executing the next steps of script.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class ImplicitWaitTest {

	protected WebDriver driver;

	@Test
	public void implicitWaitTest () throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		// navigate to web site
		driver.get("https://makeinjava.com");
		// maximize the browser window
		driver.manage().window().maximize();
		// prints the title of web page
		System.out.println(driver.getTitle());
		driver.close();
	}
}
  • There can be some steps in the script that may take longer time (e.g: 60 seconds) to execute like While booking a flight, it will fetch all the available flights. In such cases, it is advisable to use Explicit Wait instead of implicit wait to avoid performance issues.

Scroll to Top