Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sleep Selenium WebDriver in Python for milliseconds

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?

like image 901
AliRehman7141 Avatar asked Oct 02 '18 07:10

AliRehman7141


People also ask

How do you sleep in milliseconds Python?

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.

How do I sleep in Selenium?

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.

How do you call sleep in Python?

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.

How does time sleep work in Python?

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.


2 Answers

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

like image 176
undetected Selenium Avatar answered Sep 23 '22 20:09

undetected Selenium


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)
like image 27
fam Avatar answered Sep 25 '22 20:09

fam