Difference Between Implicit Wait and Explicit Wait in Selenium

0
46

This tutorial is all about Difference Between Implicit Wait and Explicit Wait in Selenium.
An explicit wait will wait for a certain condition to occur before proceeding further in the code.There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

Below is the standard code for Explicit Wait:

  1. WebDriver driver = new FirefoxDriver();
  2. driver.get("http://somedomain/url_that_delays_loading");
  3. WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  4. .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
  5.  
  6. //presenceOfElementLocated is a method inside ExpectedConditions class. You can use any
  7. //other method apart from this based on your requirement.

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 – 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Implicit Wait:

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Below is the standard code for Implicit Wait:

  1. WebDriver driver = new FirefoxDriver();
  2. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  3. driver.get("http://somedomain/url_that_delays_loading");
  4. WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

 

Readers might read also:

 

 

Facebook Comments
(Visited 57 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here