当前位置:网站首页>元素等待机制
元素等待机制
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())
边栏推荐
- WBF: new method of NMS post filter frame for detection task?
- 【日常训练】535. TinyURL 的加密与解密
- Sd6.23 summary of intensive training
- 行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨
- Mac: MySQL 66 questions, 20000 words + 50 pictures!
- 第八届“互联网+”大赛 | 云原生赛道邀你来挑战
- Sd6.24 summary of intensive training
- How to use the oak camera on raspberry pie?
- 源码安装MAVROS
- Tag filtering and SQL filtering of rocketmq
猜你喜欢

1. use stm32cubemx to establish stm32g030c8t6 project

How to use the low code platform of the Internet of things for service management?

报错Failed to allocate graph: MYRIAD device is not opened.

Adobe Premiere foundation - opacity (matte) (11)

关于微服务

C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad

jdbc_相關代碼

Understanding of strong caching and negotiation caching

RocketMQ的tag过滤和sql过滤

山东大学项目实训(七)添加导航栏选择城市
随机推荐
JDBC Codes connexes
UnrealEngine插件中使用protobuf 链接mysql
Source code installation mavros
数据仓库模型分层ODS、DWD、DWM实战
Data warehouse model layered ODS, DWD, DWM practice
Failed to allocate graph: myriad device is not opened
Dragon canvas animation
CentOS 7.5 install MySQL 8.0.27---yum
Fastdfs
Shell基本语法--流程控制
Interview question 10.10 Rank of digital stream
The 8th "Internet +" competition - cloud native track invites you to challenge
JDBC knowledge
Cannot retrieve repository metadata processing records
jdbc_相关代码
Shandong University project training (VI) Click event display line chart
Redis bloom filter and cuckoo filter
C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
MySQL -connector/j driver download
第八届“互联网+”大赛 | 云原生赛道邀你来挑战