Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python & Selenium: Difference between driver.implicitly_wait() and time.sleep()

Yes, I know both are used to wait for some specified time.

Selenium:

driver.implicitly_wait(10)

Python:

import time
time.sleep(10)

Is there any difference between these two?

like image 379
Dipankar Nalui Avatar asked Dec 03 '18 07:12

Dipankar Nalui


People also ask

What is A += in python?

The Python += operator lets you add two values together and assign the resultant value to a variable. This operator is often referred to as the addition assignment operator.

What is the basic language of Python?

Python is written in C (actually the default implementation is called CPython).

Is Python hard to learn?

Python is widely considered among the easiest programming languages for beginners to learn. If you're interested in learning a programming language, Python is a good place to start. It's also one of the most widely used.


1 Answers

time.sleep(secs)

time.sleep(secs) suspends the execution of the current thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

You can find a detailed discussion in How to sleep webdriver in python for milliseconds


implicitly_wait(time_to_wait)

implicitly_wait(time_to_wait) 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 SECONDS 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
  • ElementClickInterceptedException
  • ElementNotInteractableException

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.

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

like image 63
undetected Selenium Avatar answered Sep 21 '22 00:09

undetected Selenium