我在脚本中使用time
库:
import time
time.sleep(1)
它可以使我的Web驱动程序休眠1秒钟,但我需要使其休眠250毫秒。
答案 0 :(得分:1)
答案 1 :(得分:1)
如果您希望它在毫秒内进入睡眠状态,请使用浮点值:
import time
time.sleep(0.25)
#0.25 > 250ms
#0.1 > 100ms
#0.05 > 50ms
答案 2 :(得分:1)
要暂停网络驱动程序的执行毫秒,您可以通过以下方式传递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
但是,当使用 time.sleep(secs)
使用 Selenium 和 WebDriver 进行自动化时,没有任何要达到的特定条件 违反了自动化 的目的,因此应不惜一切代价避免。根据文档:
time.sleep(secs)
在给定的秒数内暂停当前线程的执行。该参数可以是浮点数,以指示更精确的睡眠时间。实际的暂停时间可能少于请求的暂停时间,因为任何捕获到的信号都会在执行该信号的捕获例程后终止sleep()。另外,由于系统中其他活动的安排,暂停时间可能比请求的时间长任意数量。
因此,按照讨论而不是time.sleep(sec)
,您应将WebDriverWait()
与expected_conditions()
结合使用以验证元素的状态,并且三个被广泛使用的Expected_conditions如下:
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(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(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.