How to sleep Selenium WebDriver in Python for milliseconds How to sleep Selenium WebDriver in Python for milliseconds selenium selenium

How to sleep Selenium WebDriver in Python for milliseconds


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

import timetime.sleep(1) #sleep for 1 sectime.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 locatedDescription : 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 visibleDescription : 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


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


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

import timetime.sleep(0.25)#0.25 > 250ms#0.1  > 100ms#0.05 > 50ms