一.强制等待

1.设置完等待后不管有没有找到元素,都会执行等待,等待结束后才会执行下一步

2.实例

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(3) # 设置强制等待
driver.quit()

二.隐性等待

1.设置全局等待,对每个查询的元素都生效,在等待时间内找到了该元素则执行下一步,未找到报错。

2.实例

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.implicitly_wait(10) # 设置隐性等待
driver.quit() 

三.显性等待

1.WebDriverWait类

1)导入webdriverwait类

from selenium.webdriver.support.wait import WebDriverWait

2)实例化WebDriverWait

wait = WebDriverWait(driver, 10, 2)  # 10为等待时间,2为在10s内每过2s去判断一次

selenium提供了WebdriverWait类用于针对指定的元素设置等待,其中内含until和until_not两个方法判断

3)until(self, method, message: str = "")  函数

methon:为判断条件,若返回true,则判断成功,返回false,判断失败,打印message信息

message:为判断失败时打印的信息,可写可不写。

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until("判断条件", "返回false时打印的信息")
driver.quit()

4)until_not(self, method, message: str = "") 函数

until_not效果与until相反,返回false时判断成功,返回true时判断失败

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until_not("判断条件", "返回true时打印的信息")
driver.quit()

5)判断条件通常与expected_conditions连用,内部封装了判断方法。expected_conditions的具体用法,我们接着往下看。

2.expected_conditions

下面介绍expected_conditions模块下所有的函数用法

1)title_is:精准匹配页面标题,匹配成功返回true,失败返回false

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until(title_is("百度一下,你就知道")) # 精准匹配标题
driver.quit()

2)title_contains:模糊匹配标题,匹配成功返回true,失败返回false

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until(title_contains("百度")) # 模糊匹配标题
driver.quit()

3)presence_of_element_located:判断定位的元素是否存在(可见和隐藏元素),存在返回true,否则返回false

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until(presence_of_element_located((By.ID, "kw")), "不存在") # 判断元素是否存在,可见和隐藏元素都可判断
driver.quit()

4)url_contains:判断页面url地址是否包含预期结果,满足预期返回true,不满足返回false

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until(url_contains("baidu1"), "不包含") # 检测当前页面url地址是否包含预期结果
driver.quit()

5)url_matches:判断当前页面地址是否包含预期结果,内填写正则表达式,满足预期返回true,不满足返回false

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until(url_matches("baidu"), "不包含") # 检测当前页面url地址是否包含预期结果,内填写正则表达式
driver.quit()

6)url_to_be:精准判断url,若相同返回true,不同返回false

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until(url_to_be("https://www.baidu.com/"), "不存在") # 精准判断url
driver.quit()

7)url_changes:精准判断url,若相同返回false,不同返回true

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until(url_changes("https://www.baidu.c"), "相等") # 精准匹配url不相等
driver.quit()

8)visibility_of_element_located:判断定位的元素是否存在,只能判断可见元素,存在返回true,不存在返回false

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
wait.until(visibility_of_element_located((By.ID, "kw")), "不存在") # 判断元素是否存在,只适用于可见元素
driver.quit()

9)visibility_of:判断元素是否存在,只能判断可见元素

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import * option = webdriver.ChromeOptions()
option.add_argument("--headless") # 设置无窗口模式
driver = webdriver.Chrome(options=option)
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver, 10, 2) # 设置显性等待
element_id = driver.find_element(by=By.ID, value="kw")
wait.until(visibility_of(element_id), "不存在") # 判断元素是否存在,只适用于可见元素
driver.quit()

此方法与visibility_of_element_located判断结果相同,只是传递参数不同,visibility_of传元素,visibility_of_element_located传元组

10)presence_of_all_elements_located:判断页面至少有一个定位的元素存在(可见和隐藏元素都会判断)

wait.until(presence_of_all_elements_located((By.TAG_NAME, "span")), "没有一个存在")  # 判断页面至少有一个定位的元素存在(可见和隐藏元素都会判断)

11)visibility_of_any_elements_located:判断页面至少有一个定位的元素存在,且为可见元素

wait.until(visibility_of_any_elements_located((By.TAG_NAME, "span")), "没有一个存在")  # 判断页面至少有一个定位的元素存在,且为可见元素

12)visibility_of_all_elements_located:判断定位的元素全部可见

wait.until(visibility_of_all_elements_located((By.TAG_NAME, "span")), "不可见")  # 判断定位的元素全部可见

13)text_to_be_present_in_element:模糊匹配文本值

wait.until(text_to_be_present_in_element((By.XPATH, "//span[contains(text(),'123')]"), "124"), "匹配不成功")  # 模糊匹配元素文本值

14)text_to_be_present_in_element_value:模糊匹配定位元素的value值

wait.until(text_to_be_present_in_element_value((By.XPATH, "//input[@id='su']"), "百度一下"), "匹配错误")  # 模糊匹配元素value值

15)text_to_be_present_in_element_attribute:模糊匹配定位元素指定属性的属性值

wait.until(text_to_be_present_in_element_attribute((By.XPATH, "//input[@id='kw']"), "name", "w"), "匹配错误")  # 模糊匹配定位元素指定属性的属性值

16)frame_to_be_available_and_switch_to_it:判断frame是否可以切换(switch_to.frame())

wait.until(frame_to_be_available_and_switch_to_it((By.XPATH, "elenment")), "不可切换")  # 判断frame是否可以切换

17)invisibility_of_element_located:判断定位的元素是否不可见或者不存在,不可见返回true,反之返回false

wait.until(invisibility_of_element_located((By.TAG_NAME, "span")), "错误")  # 判断元素是否不可见/不存在,不可见返回true

18)invisibility_of_element:判断元素是否不可见或者不存在,不可见返回true,反之返回false

span=driver.find_element(By.TAG_NAME, "span")
wait.until(invisibility_of_element(span), "错误") # 判断元素是否不可见或者不存在,不可见返回true,反之返回false

与invisibility_of_element_located用法相同,只是传递参数不同,一个传元素,一个传元组

19)element_to_be_clickable:判断定位的元素是否可点击

wait.until(element_to_be_clickable((By.ID, "su")), "错误")  # 判断定位的元素是否可点击

20)staleness_of:判断元素是否存在,存在若在等待的时间内被移除,则返回true

span = driver.find_element(By.ID, "su")
wait.until(staleness_of(span), "错误") # 判断元素是否存在,存在若在等待的时间内被移除,则返回true

这里注意的是传递的参数是元素

21)element_to_be_selected:判断元素是否被选中

id=driver.find_element(by=By.XPATH, value="//option[contains(text(),'2')]")
wait.until(element_to_be_selected(id),"失败") # 判断可见元素是否选中

这里注意的是传递的参数是元素

22)element_located_to_be_selected:判断定位的元素是否被选中,选中返回true,未选中返回false

wait.until(element_located_to_be_selected((By.XPATH, "//option[contains(text(),'1')]")),"失败")  # 判断定位的元素是否被选中

与element_to_be_selected用法相同,不同的是传递的是元组

23)element_selection_state_to_be:判断元素选中的状态是否符合预期

id=driver.find_element(by=By.XPATH, value="//option[contains(text(),'2')]")

wait.until(element_selection_state_to_be(id,False),"选中了") # 判断元素是否被选中,并给出预期结果

24)element_located_selection_state_to_be:判断元素选中的状态是否符合预期

wait.until(element_located_selection_state_to_be((By.ID,"su"),True),"错误")   # 判断定位的元素是否被选中,并给出预期结果

与element_selection_state_to_be用法相同,不同的是传递的元组

25)number_of_windows_to_be:判断当前打开的窗口是否符合预期

wait.until(number_of_windows_to_be(1),"不是一个")  # 期望当前打开的窗口数为几个

26)new_window_is_opened:判断是否新打开了一个窗口

hand = driver.window_handles  # 获取当前所有窗口的柄句
print(len(hand))
driver.find_element(by=By.XPATH, value="//a[contains(text(),'新闻')]").click()
wait.until(new_window_is_opened(hand)) # 判断是否打开了一个新窗口

27)alert_is_present:判断页面是否有alert

wait.until(alert_is_present(),"没有alert")  # 判断页面是否有alert

28)element_attribute_to_include:判断定位的元素是否存在预期的属性值

这个我们就不做多余的介绍了,因为本身封装的就有问题,我们先来看下封装的原代码

通过get_attribute(attribute_)获取属性值,若为none则返回false,否则返回不为none,其实这点是存在问题的

因为get_attribute(attribute_)当属性不存在时是什么都不会返回的,更不会返回none。

29)any_of:判断多个条件满足一个为true的话就返回true,相当于or逻辑

wait.until(any_of(alert_is_present(), element_attribute_to_include((By.TAG_NAME, "a"), "name")), "没有一个符合要求的")  # 多个判断条件有一个返回true,则返回True,or逻辑

30)all_of:判断多个条件必须都满足为true的话才返回true,相当于and逻辑

wait.until(all_of(alert_is_present(), element_attribute_to_include((By.TAG_NAME, "a"), "name")))  # 多个判断条件必须都满足,True,and逻辑

31)none_of:判断多个条件都返回false时,才能判断成功返回true

wait.until(none_of(alert_is_present(), element_attribute_to_include((By.TAG_NAME, "a"), "name")))  # 判断多个条件都返回flase时返回true,有一个返回true时则返回false

  

 

文章来源:https://www.cnblogs.com/lihongtaoya/ ,请不要转载

史上最全的selenium三大等待介绍的更多相关文章

  1. 史上最全!Selenium元素定位的30种方式

    Selenium对网页的控制是基于各种前端元素的,在使用过程中,对于元素的定位是基础,只有准去抓取到对应元素才能进行后续的自动化控制,我在这里将对各种元素定位方式进行总结归纳一下. 这里将统一使用百度 ...

  2. Istio技术与实践06:史上最全!Istio安装参数介绍

    一. CertManage Istio-1.0版本新加入的组件,利用ACME为Istio签发证书 Key Default Value Description certmanager.enabled T ...

  3. nacos 实战(史上最全)

    文章很长,而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三 ...

  4. Java基础面试题(史上最全、持续更新、吐血推荐)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  5. Linux面试题(史上最全、持续更新、吐血推荐)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  6. 史上最全Windows版本搭建安装React Native环境配置

    史上最全Windows版本搭建安装React Native环境配置 配置过React Native 环境的都知道,在Windows React Native环境配置有很多坑要跳,为了帮助新手快速无误的 ...

  7. 【Tips】史上最全H1B问题合辑——保持H1B身份终级篇

    [Tips]史上最全H1B问题合辑——保持H1B身份终级篇 2015-04-10留学小助手留学小助手 留学小助手 微信号 liuxue_xiaozhushou 功能介绍 提供最真实全面的留学干货,帮您 ...

  8. 开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发

    [原][开源框架]Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位... 时间 2015-01-05 10:08:18 我是程序猿,我为自己代言 原文  http: ...

  9. 史上最全的 Java 新手问题汇总

    史上最全的 Java 新手问题汇总   Java是目前最流行的编程语言之一——它可以用来编写Windows程序或者是Web应用,移动应用,网络程序,消费电子产品,机顶盒设备,它无处不在. 有超过30亿 ...

随机推荐

  1. 快速入门python看过的一些资料

    我快速入门python看过的一些资料 B站的视频 10天自学Python,轻松掌握Python基础[千锋] 廖雪峰 - Python教程 https://www.liaoxuefeng.com/wik ...

  2. mysql导出导入数据库和表学习笔记

    一.mysql导出数据库和表 1.导出单个数据库 mysqldump [-h Host] -u Username -p [PASSWORD] db_name > db_name.sql 2.导出 ...

  3. js实现全屏弹框

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. 6.6 NOI 模拟

    \(T1\)括号序列 --那是,朝思夜想也未尝得到的自由 一个比较常见的转化,考虑如何判断前一段和后一段能够拼成一个合法的括号序列 充要条件: 前半部分,'('看为\(1\), ')'看为\(-1\) ...

  5. flutter系列之:flutter架构什么的,看完这篇文章就全懂了

    目录 简介 Flutter的架构图 embedder engine Flutter framework Widgets Widgets的可扩展性 Widgets的状态管理 渲染和布局 总结 简介 Fl ...

  6. C#/VB.NET 替换 PDF 文件上的现有图像

    我们都知道对PDF文件进行修改和编辑不是一件容易的事.但有时当我们想用新的图像来替换PDF文件上的现有图像时,该怎么办呢?别担心,本文将向您展示如何在 C#/VB.NET 中替换 PDF 文件中的现有 ...

  7. Spring Boot部署方法

    Spring Boot部署方法     网上搜到的部署方法无非是打成jar包,然后shell执行nohup java调用jar命令,或者是打成war包然后部署到tomcat或者jetty容器上面. S ...

  8. 【python】pandas 索引操作

    选择.修改数据(单层索引) 推荐使用.at..iat..loc..iloc 操作 句法 结果 备注 选择列 df[col] Series 基于列名(列的标签),返回Series 用标签选择行 df.l ...

  9. 【BZOJ2658】[Zjoi2012]小蓝的好友(mrx) (扫描线,平衡树,模拟)

    题面 终于到达了这次选拔赛的最后一题,想必你已经厌倦了小蓝和小白的故事,为了回馈各位比赛选手,此题的主角是贯穿这次比赛的关键人物--小蓝的好友. 在帮小蓝确定了旅游路线后,小蓝的好友也不会浪费这个难得 ...

  10. 面试突击78:@Autowired 和 @Resource 有什么区别?

    @Autowired 和 @Resource 都是 Spring/Spring Boot 项目中,用来进行依赖注入的注解.它们都提供了将依赖对象注入到当前对象的功能,但二者却有众多不同,并且这也是常见 ...