Using implicit wait in selenium

0
0

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")
  • You must to post comments
0
0

ImplicitWait

ImplicitWait as per the official documentation is to configure the WebDriver instance i.e. the driver to poll the HTML DOM for a certain amount of time (interms 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:

  • NoSuchElementException
  • TimeoutException
  • ElementNotVisibleException
  • ElementNotSelectableException

Hence we introduce ImplicitWait. By introducing ImplicitWait the driver will poll the HTML DOM for the configured amount of time looking out for the element or elements. 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);

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);

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);

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.
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website