如何在Python中让Selenium WebDriver休眠毫秒数

18

我在我的脚本中使用了time库:

import time
time.sleep(1)

它可以让我的Selenium WebDriver休眠一秒钟,但是如何做到250毫秒呢?


类似于这个问题 如何让我的Python程序睡眠50毫秒? - sa_leinad
1
这与Selenium WebDriver(?)毫无关系。它是关于Python中的sleep,因此可能有数不清的重复问题(超过10年的价值)。其中的规范是*如何让我的Python程序休眠50毫秒?*(尽管没有一个答案真正解决了亚秒级睡眠的16.66毫秒时间分辨率的常见问题)- 可能在这种特殊情况下不是问题。 - Peter Mortensen
计算机的普通处理器要想精确计算时间是非常困难(如果不是不可能的话),因此一秒钟是可以用合理精度测量的时间。如果你想要小于一秒钟的短暂停顿,也许打开/关闭磁盘上的一个小文件需要大约那么长的时间。 - Amin
4个回答

25

要暂停Webdriver的执行若干毫秒,您可以按以下方式传递秒数浮点秒数

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

然而,当使用 SeleniumWebDriver 进行自动化时,使用 time.sleep(secs) 而没有任何特定条件来实现会 破坏了自动化的目的,应该尽一切可能避免使用。 根据文档:

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


因此,根据讨论,您应该使用 WebDriverWait()expected_conditions() 结合使用来验证元素状态,其三个广泛使用的 expected_conditions 如下:

presence_of_element_located

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

visibility_of_element_located

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

参考资料

您可以在WebDriverWait not working as expected中找到详细讨论。



1
很好的解释了在使用selenium时,sleep和你应该使用什么代替sleep。当我刚开始使用selenium时,我曾经使用sleep来帮助加载时间,但是在使用expected_conditions之后,我的selenium脚本更加稳定,需要的维护也更少。非常有用的信息! - PixelEinstein
1
避免“不惜一切代价”地使用睡眠是过于强硬的,这会假定最短完成时间是首要考虑的极为罕见的情况。实际上,我几乎永远不会编写没有大量睡眠的Selenium自动化测试。如果你不这样做,你就有可能使CPU/驱动器/网络使用率达到最大值,这通常是不好的。相反,如果你减慢一切,你将能够更好地管理你的资源,而且对Web服务器的负载也会大大降低。 - Corvus

5

time.sleep()需要一个浮点数参数:

time.sleep(0.25)

文档(值得一读,因为它们解释了导致睡眠时间比预期短或长的条件)。


3
如果您希望它以毫秒为单位休眠,那么请使用浮点值:
import time
time.sleep(0.25)

#0.25 > 250ms
#0.1  > 100ms
#0.05 > 50ms

1
理论上,time.sleep(0.25) 会引起250毫秒的等待。然而,实际等待时间可能比精确的250毫秒短或长。这是因为:

由于任何捕获的信号都将终止执行该信号捕获例程后的睡眠(),因此实际挂起时间可能小于所请求的时间。此外,由于系统中其他活动的安排,挂起时间可能比所请求的时间长。

使用selenium进行等待的其他方法包括:
  1. 隐式等待:driver.implicitly_wait(0.25)
  2. 显式等待:WebDriverWait(driver).until(document_initialised)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接