Implicit / explicit wait not waiting for specified amount of time

1.2k Views Asked by At

I am trying to do a sendKeys() to a text field , which can be accomplished by Thread.sleep() ( which I want to avoid ) . Now I have used implicit wait of 5 - 10 seconds but the execution visibly is not waiting for that amount of time . Adding explicit wait with expected conditions of elementToBeClickable() results similar intermittent failure.

2

There are 2 best solutions below

0
On

If you are able to invoke sendKeys() to a text field after invoking Thread.sleep() essencially implies that the real issue is with the implementation of implicit wait and/or WebDriverWait


Deep Dive

While interacting with elements of an application based on JavaScript, ReactJS, jQuery, AJAX, Vue.js, Ember.js, GWT, etc. implicit wait isn't that effective.

Insuch cases you may opt to remove implicit wait completely with WebDriverWait as the documentation of Waits clearly mentions:

Warning: 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.


Solution

First you need to reconfigure implicit wait to 0 as follows:

  • Python:

    driver.implicitly_wait(0)
    
  • Java:

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

    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
    

Instead induce WebDriverWait for the elementToBeClickable() as follows:

  • Python:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "elementID"))).send_keys("Debajyoti Sikdar")
    
  • Java:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("elementID"))).sendKeys("Debajyoti Sikdar");
    
  • DotNet:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.entry#ng-touched[id='limit'][name='limit']"))).SendKeys("Debajyoti Sikdar");
    

References

You can find a detailed discussion in:

0
On

While defining explicit wait, please make sure that you are adding correct Expected Conditions.

You can check this "https://itnext.io/how-to-using-implicit-and-explicit-waits-in-selenium-d1ba53de5e15".