4

I am a beginner. I understand what waits basically does but I am confused over how different tutorials over the internet place it and explain it. For example, in the below code it is placed before loading the URL. So, is it only to wait for the URL to be loaded or for finding the element or both? Is is true that if I use an implicit wait once in my try block, it will be applicable for every element search I am performing in my code?

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Shivam Mishra
  • 1,731
  • 2
  • 11
  • 29
  • Please read the official docs to understand how it works, http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits. – JeffC Aug 14 '17 at 15:58
  • @Concerned folks, please convey the motivation behind downvoting this question. – Shivam Mishra Jul 07 '18 at 13:39

3 Answers3

8

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 Framework may be facing any of these exceptions:

Hence we introduce ImplicitWait. By inducing 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);
    

Answering 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.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    According to the selenium [WebDriver documentation](http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits), it is not possible to reconfigure an implicit wait: "Once set, the implicit wait is set for the life of the WebDriver object instance." – Simon Feb 07 '18 at 15:01
  • 1
    @Simon What that means is that once it's set, it doesn't change for the life of the driver instance. It *can* be changed if you change it. You can test this for yourself... it works. You can essentially turn off implicit wait by setting it to 0 and then "turn it back on" by setting it to some non-zero value. – JeffC Jul 07 '18 at 14:58
3

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-

Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
  • You said its valid for all the elements, is it applicable for operations like loading the URL as well? And I suppose I have to use explicit wait for every element I need to look for because its condition based. – Shivam Mishra Aug 14 '17 at 10:55
  • there is not direct way to do this for every element, you could write your own function `i.e. click();` which does two things. 1. explicit wait with specific condition and 2. `click()` – Gaurang Shah Aug 14 '17 at 10:57
  • Lets suppose say I am trying to find multiple elements through explicit wait in a single try block or a function, is writing it once enough? What will be the case when I am writing other function/try block? – Shivam Mishra Aug 14 '17 at 11:06
  • how could i say that, it depends what you write in that function. – Gaurang Shah Aug 14 '17 at 11:16
2

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.

Arun Ghosh
  • 21
  • 3