0

As per my knowledge, the explicit wait is a conditional wait and it would continue to execute the next step if the element gets found before the time specified. As shown in the code, the implicit wait should not execute next step even if the element gets found before 10secs, but it immediately execute next step i.e.within 2secs. It would not continue to poll DOM for entire 10secs. Also please suggest how to decide which wait to use as per examples? I have gone through many questions but all are copy paste, expecting answers at the architecture level.

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String appUrl = "https://www.facebook.com/";
driver.get(appUrl);
driver.manage().window().maximize();
WebElement username = driver.findElement(By.xpath("//input[@id='email']"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    Possible duplicate of [What is difference between Implicit wait Vs. Explicit wait in selenium webdriver?](https://stackoverflow.com/questions/22656615/what-is-difference-between-implicit-wait-vs-explicit-wait-in-selenium-webdriver) – Ishita Shah Dec 12 '18 at 07:39

1 Answers1

0

ImplicitWait

ImplicitWait is to specify the amount of time the WebDriver instance i.e. the driver should wait when searching for an element if it is not immediately present in the HTML DOM in-terms of NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS or DAYS when trying to find an element or elements if they are not immediately available. The default setting is 0 which means the driver when finds an instruction to find an element or elements, the search starts and results are available on immediate basis.

In this case, after a fresh loading of a Webpage an element or elements may be / may not be found on an immediate search. So your Automation Script may be facing any of these exceptions:

Hence we introduce ImplicitWait. By introducing ImplicitWait the driver will poll the DOM Tree until the element has been found for the configured amount of time looking out for the element or elements before throwing a NoSuchElementException. By that time the element or elements for which you had been looking for may be available in the HTML DOM. As in your code you have already set ImplicitWait to a value of 10 seconds, the driver will poll the HTML DOM for 10 seconds.

  • Java:

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

Finally, once you set the ImplicitWait, the WebDriver instance i.e. the driver is able to carry this configuration till its lifetime. But if you need to change the coarse of time for the WebDriver instance i.e. the driver to wait then you can reconfigure it as follows:

  • Java:

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

If at any point of time you want to nullify the ImplicitWait you can reconfigure it as follows:

  • Java:

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

You can find a detailed discussion in Using implicit wait in selenium


ExplicitWait

ExplicitWait is a code block you define, configure and implement for the WebDriver instance to wait for a certain condition to be met before proceeding for the next line of code. There are some methods that helps us to implement ExplicitWait that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one of the way ExplicitWait can be achieved.

An Example:

  • Java:

    driver.get("http://www.example.com/");
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_css"))).click();
    

Explanation:

This implementation of ExplicitWait waits up to 10 seconds before throwing a TimeoutException or if it finds the element then it will return within 0 to 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true or a not-null object.

Expected Conditions:

There are some frequently encountered conditions when automating Web Browsers for Testing Web/Mobile Applications. The Java, C# and Python bindings include those convenient methods so we don’t have to write-up an ExpectedCondition class ourselves or create our own utility package for them. Some of the Expected Conditions are:

  • alertIsPresent()
  • elementToBeClickable(locator)
  • elementToBeSelected(WebElement)
  • frameToBeAvailableAndSwitchToIt(locator)
  • invisibilityOf(element)

Here you can find about the all the methods supported by Expected Conditions.

You can find a detailed discussion in Replace implicit wait with explicit wait (selenium webdriver & java).


Note : Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352