How to sleep Selenium WebDriver in Python for milliseconds

23,381

Solution 1

To suspend the execution of the webdriver for milliseconds you can pass number of seconds or floating point number of seconds as follows:

import time
time.sleep(1) #sleep for 1 sec
time.sleep(0.25) #sleep for 250 milliseconds

However while using Selenium and WebDriver for Automation using time.sleep(secs) without any specific condition to achieve defeats the purpose of Automation and should be avoided at any cost. As per the documentation:

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.


So as per the discussion instead of time.sleep(sec) you should use WebDriverWait() in-conjunction with expected_conditions() to validate an element's state and the three widely used expected_conditions are as follows:

presence_of_element_located

presence_of_element_located(locator) is defined as follows :

class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)

Parameter : locator - used to find the element returns the WebElement once it is located

Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or interactable (i.e. clickable). 

visibility_of_element_located

visibility_of_element_located(locator) is defined as follows :

class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)

Parameter : locator -  used to find the element returns the WebElement once it is located and visible

Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

element_to_be_clickable

element_to_be_clickable(locator) is defined as follows :

class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)

Parameter : locator - used to find the element returns the WebElement once it is visible, enabled and interactable (i.e. clickable).

Description : An Expectation for checking an element is visible, enabled and interactable such that you can click it. 

Reference

You can find a detailed discussion in WebDriverWait not working as expected

Solution 2

time.sleep() takes a floating-point argument:

time.sleep(0.25)

Docs (they're worth a read not least because they explain the conditions under which the sleep could end up being shorter or longer than expected).

Solution 3

If you want it to sleep in milliseconds then use float values:

import time
time.sleep(0.25)

#0.25 > 250ms
#0.1  > 100ms
#0.05 > 50ms

Solution 4

Theoretically, time.sleep(0.25) induces a wait of 250ms. However, the actual wait may be shorter or longer instead of being precisely 250ms. This is because:

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.

Other methods of using wait with selenium include:

  1. Implicit wait: driver.implicitly_wait(0.25)
  2. Explicit wait: WebDriverWait(driver).until(document_initialised)
Share:
23,381
AliRehman7141
Author by

AliRehman7141

I am working as Senior React Native Application Developer at Appcrates Software House Gujranwala. I have 3 years of experience in React Native application development.

Updated on July 05, 2022

Comments

  • AliRehman7141
    AliRehman7141 almost 2 years

    I am using the time library in my script:

    import time
    time.sleep(1)
    

    It can sleep my Selenium WebDriver for one second, but how is it possible for 250 milliseconds?

  • PixelEinstein
    PixelEinstein over 5 years
    Good explanation of both sleep and what you should use instead of sleep with selenium. I used to use sleep to help with load times when I first started with selenium, but after using expected_conditions, my selenium scripts are much more stable and require less maintenance. Good Info!
  • Corvus
    Corvus about 4 years
    Avoiding sleep at "any cost" is way too strong, and would presume the exceptionally rare situation where shortest time to complete is a priority. In fact I would almost NEVER write a selenium automatrion without a liberal sprinkling of sleeps thoughout. if you don't you risk maxing out the CPU/Drive/Network usage which is generally a bad thing. Instead if you slow everything down you will be able to manage your resources a lot better, plus you are being considerably less load on the webserver.