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?
To sleep for milliseconds with this method, simply use a fractional number. To sleep for 400 milliseconds, for example, use time. sleep(0.4), use time for 60 milliseconds sleep(0.06), for example. Python's sleep() function is a part of the time package.
sleep() causes the current thread to suspend execution for a specified period. Thread. sleep() is not a Selenium wait, it is provided by Java. It suspends the code for the specified time and can be useful in debugging the script under test.
If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.
Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, not the whole program.
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(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(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(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.
You can find a detailed discussion in WebDriverWait not working as expected
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:
driver.implicitly_wait(0.25)
WebDriverWait(driver).until(document_initialised)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With