当前位置:网站首页>元素等待机制
元素等待机制
2022-06-29 18:29:00 【是乔乔啊】
背景介绍
很多测试场景下,我们需要是脚本的运行速度和程序的响应速度保持一致。因此,WebDriver提供了隐式等待和显示等待两种机制。
隐式等待
对于解决由于网络延迟或利用Ajax动态加载元素所导致的程序响应时间不一致,是非常有效的。
设置了隐式等待时间后,WebDriver会在一定时间内持续检测和搜寻DOM,以便于查找一个或多个不是立即加载成功并可用的元素。
一般情况下,隐式等待的默认超时时间设置为0。
self.driver.implicitly_wait(30)
超时仍未找到元素,则会抛出NoSuchElementException的异常。
显示等待
显示等待比隐式等待具备更好的操控性。
显示等待可以只作用于仅有同步需求的测试用例。
WebDriver提供了WebDriverWait类和expected_conditions类来实现显示等待。
如下两种用法:
WebDriverWait(self.driver, 10).until(lambda s:s.find_element_by_id("select-language").get_attribute("length")=="3")
account = WebDriverWait(self.driver, 10) \
.until(expected_conditions. \
visibility_of_element_located \
((By.LINK_TEXT, "ACCOUNT")))
account.click()
如果超时,元素仍未可见,则会抛出TimeoutException异常。
expected_conditions类
常用等待条件如下:


判断某个元素是否存在
可以使用element_to _be_clickable等待并检查一个元素是否可用。当目标元素可点击或可用的时候,该方法返回定位到的目标元素给测试脚本。
也可以通过检测标题是否出现指定文案作为判断条件,例如我们可以使用title_contains预期条件检测。
判断是否存在Alerts
显示等待同样可以应用在警告和页面框架中。Alert_is_present 预期判定条件就可以用来检测警告窗口是否出现,并且把警告窗口返回给脚本,以进行后续的动作。
alert = WebDriverWait(self.driver, 10).until(expected_conditions.alert_is_present())
边栏推荐
- JDBC knowledge
- Apache inlong million billion level data stream processing
- 优雅书写Controller(参数验证+统一异常处理)
- Apache Doris 基本使用总结
- 什么是多范式编程语言,其中的“多范式”是什么意思?
- Usage of BeanUtils property replication
- 金鱼哥RHCA回忆录:DO447构建高级作业工作流--创建工作流作业模板并启动工作流作业
- 对强缓存和协商缓存的理解
- Failed to allocate graph: myriad device is not opened
- Error building sqlsession problem
猜你喜欢

UnrealEngine插件中使用protobuf 链接mysql

Failed to allocate graph: myriad device is not opened

数据仓库模型分层ODS、DWD、DWM实战

Dragon canvas animation

行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨
![[how the network is connected] Chapter 3 explores hubs, switches and routers](/img/a9/39f7c474331b7de0bdaf6e59f0d15b.png)
[how the network is connected] Chapter 3 explores hubs, switches and routers

Adobe Premiere Basics - general operations for editing material files (offline files, replacing materials, material labels and grouping, material enabling, convenient adjustment of opacity, project pa

Anaconda installs and configures jupyter notebook remote

jdbc_ Related codes

源码安装MAVROS
随机推荐
JDBC knowledge
Source code installation mavros
2. 在STM32CubeMX建立的Keil5工程中添加自定义的相关文件
Adobe Premiere foundation - batch material import sequence - variable speed and rewind (recall) - continuous action shot switching - subtitle requirements (13)
WBF: new method of NMS post filter frame for detection task?
WBF:检测任务NMS后虑框新方式?
Svg circle drawing path animation
MySQL 企業級開發規範
第八届“互联网+”大赛 | 云原生赛道邀你来挑战
Programmer Resource Recommendation Guide
如何使用物联网低代码平台进行服务管理?
Data warehouse model layered ODS, DWD, DWM practice
Machine learning 8-artificial neural network
markdown常用字体
优雅书写Controller(参数验证+统一异常处理)
C Primer Plus Chapter 12_ Storage categories, links, and memory management_ Codes and exercises
UnrealEngine插件中使用protobuf 链接mysql
美法官裁定,被控掩盖黑客行为的Uber前安全主管必须面对欺诈指控
Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
Anaconda installs and configures jupyter notebook remote