ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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"));
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!