为了账号安全,请及时绑定邮箱和手机立即绑定

如何在python中休眠Webdriver几毫秒

/ 猿问

如何在python中休眠Webdriver几毫秒

胡子哥哥 2019-11-03 16:04:29

我time在脚本中使用库:


import time

time.sleep(1)

它可以使我的webdriver休眠1秒钟,但我需要使其休眠250毫秒。


查看完整描述

3 回答

?
慕田峪4524236

要暂停执行 webdriver毫秒,您可以通过number of seconds或执行floating point number of seconds以下操作:


import time

time.sleep(1) #sleep for 1 sec

time.sleep(0.25) #sleep for 250 milliseconds

但是,在将Selenium和WebDriver用于自动化时,在time.sleep(secs)没有任何特定条件的情况下使用将达到 自动化的目的,因此应不惜一切代价避免使用。根据文档:


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


因此,按照讨论,而不是time.sleep(sec)您应该使用WebDriverWait()in-conjunction与expected_conditions()来验证元素的状态,并且三个广泛使用的Expected_conditions如下:


present_of_element_located

present_of_element_located(locator)的定义如下:


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

visible_of_element_located

visible_of_element_located(locator)定义如下:


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)定义如下:


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. 



查看完整回答
反对 2019-11-04
?
qq_笑_17

time.sleep() 接受一个浮点参数:


time.sleep(0.25)

Docs(值得一读,因为它们解释了睡眠可能比预期的短或长的条件)。


查看完整回答
反对 2019-11-04

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信