如何在 Python 内让 Selenium WebDriver 休眠几毫秒

How to sleep Selenium WebDriver in Python for milliseconds

我在脚本中使用 time 库:

import time
time.sleep(1)

它可以让我的 Selenium WebDriver 休眠一秒钟,但怎么可能休眠 250 毫秒?

time.sleep() 采用浮点参数:

time.sleep(0.25)

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

如果您希望它以毫秒为单位休眠,请使用浮点值:

import time
time.sleep(0.25)

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

暂停 webdriver 的执行 毫秒,您可以传递 number of secondsfloating point number of seconds,如下所示:

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

然而,当使用 SeleniumWebDriver 用于 Automation 时,使用 time.sleep(secs) 没有任何 特定条件来实现 达不到 自动化 的目的并且应该不惜一切代价避免。根据文档:

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.


因此,根据讨论而不是 time.sleep(sec),您应该使用 WebDriverWait() in-conjunction with 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. 

参考

您可以在

中找到详细的讨论

理论上,time.sleep(0.25) 会导致 250 毫秒的等待。但是,实际等待时间可能更短或更长,而不是精确的 250 毫秒。这是因为:

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.

其他使用 wait with selenium 的方法包括:

  1. 隐式等待:driver.implicitly_wait(0.25)
  2. 显式等待:WebDriverWait(driver).until(document_initialised)