如何在python中休眠Webdriver毫秒 [英] How to sleep webdriver in python for milliseconds

查看:220
本文介绍了如何在python中休眠Webdriver毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在脚本中使用time库:

import time
time.sleep(1)

它可以使我的Web驱动程序休眠1秒钟,但我需要使其休眠250毫秒.

解决方案

将网络驱动程序的执行暂停毫秒,您可以按以下方式传递number of secondsfloating point number of seconds:

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

但是,在没有任何特定条件的情况下,使用 time.sleep(secs) 使用 Selenium WebDriver 进行自动化时实现 违反了自动化 的目的,应不惜一切代价避免这样做.根据文档:

time.sleep(secs) 在给定的秒数内暂停当前线程的执行.该参数可以是浮点数,以指示更精确的睡眠时间.实际的暂停时间可能少于请求的暂停时间,因为任何捕获到的信号都会在执行该信号的捕获例程后终止sleep().另外,由于系统中其他活动的安排,暂停时间可能比请求的时间长任意数量.


因此,按照讨论而不是time.sleep(sec),您应该使用 WebDriverWait无法正常工作

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 it for 250 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 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

这篇关于如何在python中休眠Webdriver毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆