How to sleep webdriver in python for milliseconds

前端 未结 3 1111
清酒与你
清酒与你 2020-11-22 04:36

I am using the time library in my script:

import time
time.sleep(1)

It can sleep my webdriver for 1 second but I need to sleep

相关标签:
3条回答
  • 2020-11-22 04:46

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

    0 讨论(0)
  • 2020-11-22 04:54

    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

    0 讨论(0)
  • 2020-11-22 05:00

    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
    
    0 讨论(0)
提交回复
热议问题