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

2024-11-20 08:44:00
admin
原创
153
摘要:问题描述:我time在我的脚本中使用该库:import time time.sleep(1) 它可以使我的 Selenium WebDriver 休眠一秒钟,但是 250 毫秒怎么可能呢?解决方案 1:要暂停webdriver 的执行number of seconds几毫秒,您可以传递或floating po...

问题描述:

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

import time
time.sleep(1)

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


解决方案 1:

要暂停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

但是,在使用SeleniumWebDriver进行自动化时,如果time.sleep(secs)没有任何特定条件,就会违背 自动化的目的,应不惜一切代价避免。根据文档:

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


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

参考

您可以在WebDriverWait 未按预期工作中找到详细讨论

解决方案 2:

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

time.sleep(0.25)

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

解决方案 3:

如果您希望它在几毫秒内进入睡眠状态,那么请使用浮点值:

import time
time.sleep(0.25)

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

解决方案 4:

理论上,time.sleep(0.25)需要等待 250 毫秒。但实际等待时间可能更短或更长,而不是恰好是 250 毫秒。这是因为:

实际暂停时间可能小于请求时间,因为任何捕获的信号都会在执行该信号的捕获例程后终止 sleep()。此外,由于系统中其他活动的调度,暂停时间可能比请求时间长任意长。

使用 selenium 进行等待的其他方法包括:

  1. 隐式等待:driver.implicitly_wait(0.25)

  2. 明确等待:WebDriverWait(driver).until(document_initialised)

相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1259  
  IPD(Integrated Product Development)流程管理作为一种先进的产品开发管理理念和方法,在提升企业创新能力方面发挥着至关重要的作用。它打破了传统产品开发过程中部门之间的壁垒,通过整合资源、优化流程,实现产品的快速、高效开发,为企业在激烈的市场竞争中赢得优势。IPD流程管理的核心概念IPD流程...
IPD流程中PDCP是什么意思   11  
  IPD(Integrated Product Development)流程管理作为一种先进的产品开发管理模式,旨在通过整合各种资源,实现产品的高效、高质量开发。在这一过程中,团队协作无疑是成功的关键。有效的团队协作能够打破部门壁垒,促进信息共享,提升决策效率,从而确保产品开发项目顺利推进。接下来,我们将深入探讨IPD流...
IPD培训课程   9  
  IPD(Integrated Product Development)研发管理体系作为一种先进的产品开发理念和方法,在众多企业中得到了广泛应用。它旨在打破部门壁垒,整合资源,实现产品开发的高效、协同与创新。在项目周期方面,IPD研发管理体系有着深远且多维度的影响,深入剖析这些影响,对于企业优化产品开发流程、提升市场竞争...
华为IPD流程   11  
  IPD(Integrated Product Development)流程管理是一种先进的产品开发管理模式,旨在通过整合企业的各种资源,实现产品的高效、高质量开发。它涵盖了从产品概念提出到产品退市的整个生命周期,对企业的发展具有至关重要的意义。接下来将详细阐述IPD流程管理的五个阶段及其重要性。概念阶段概念阶段是IPD...
IPD概念阶段   12  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用