Using implicit wait in selenium

老子叫甜甜 提交于 2019-11-26 08:58:00

ImplicitWait

ImplicitWait as per the Java Docs 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.

  • Python:

    driver.implicitly_wait(10)
    
  • Java:

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

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

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:

  • Python:

    driver.implicitly_wait(5)
    
  • Java:

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

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

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

  • Python:

    driver.implicitly_wait(0)
    
  • Java:

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

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

Answer to your questions:

  • Wait for the URL: No, ImplicitWait have no effect on page loading.
  • For finding the element: Yes, ImplicitWait will define the coarse of time the WebDriver instance will wait looking out for the element or elements.
  • Implicit wait once: Yes, you need to configure ImplicitWait only once and it is applicable throughout the lifetime of the WebDriver instance.
  • Every element search: Yes, applicable when ever findElement() or findElements() is invoked.

yes, implicit_wait is globally applicable. so once you set it's applied to all the element.

I would not suggest to use implicit_wait unless your application is too slow. You could use explicit wait or any other wait based on your requirement from the following page.

it's a JAVADOC but implementation should be same for python as well.

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#implicitlyWait-long-java.util.concurrent.TimeUnit-

Implicit wait is applicable for all the web elements where as Explicit wait is applicable only for the element it is specified.

Explicit wait is more intelligent and are really use full in handling Ajax on the other hand implicit wait is generally used to handle application sync issues.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!