当前位置:网站首页>selenium--显示等待(中)--详解篇
selenium--显示等待(中)--详解篇
2022-06-11 15:36:00 【清安无别事】
前言
这里是清安,上一章讲了显示等待的理论以及部分用法,本章我们讲一讲Expected_conditions通常也叫EC模块。
*注意:不论3.0的还是4.0的,都有这个模块。本章所用的是4.2的。
*
本章主要以封装的形式进行讲解。上车了!
倒包
from selenium.webdriver.support import expected_conditions as EC
title_is
检查页面标题的期望。title是预期的标题,必须完全匹配如果标题匹配,则返回True,否则返回false。
# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def title(self,value):
self.wait.until(EC.title_is(value),message='标题不匹配哦')
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.title('用户登录 - iWebShop商城演示')
「对以上代码做一个解析,后续都是沿用如上代码」
此处的写法可能跟你们在网上看到的不一样,不过问题不大,怎么写看自己来。主要是方法的书写这里直接将驱动、显示等待写成了类中的属性,定义了一个url方法,用于打开浏览器地址的。
title_is判断浏览器标题的,如果标题不对,则会输出message的提示信息,例如:
selenium.common.exceptions.TimeoutException: Message: 标题不匹配哦
最后又一个执行入口,在里面实例化了类以及调用类中的各种方法。需要传值就传值,不需要的直接调用
title_contains
检查标题是否包含区分大小写的子字符串。title是所需的标题片段当标题匹配时返回True,否则返回False
# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def contains_title(self,value):
self.wait.until(EC.title_contains(value))
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.contains_title('用户登录 - iWebShop商城演示')
b.contains_title('iWebShop商城演示')
这一项,用的不多,也可以用来判断是否包含指定、特定的文本,如上所示。
presence_of_element_located
检查DOM上是否存在元素的期望一页的。这并不一定意味着元素是可见的。定位器-用于查找元素找到WebElement后返回该WebElement
# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def presence_located(self,value,*ele):
el = self.wait.until(EC.presence_of_element_located(ele),message='没有发现期望的元素')
el.send_keys(value)
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.presence_located('qingan',By.NAME,'login_info')
「上述所说定位器-用于查找元素找到WebElement后返回该WebElement,所以我们在presence_of_element_located(ele)传入这个元素之后,并经过显示等待判断知道了元素存在这个界面上,那就返回这个元素,我们就可以直接拿着这个值进行下一步操作,例如上述的输入操作。」
此外,上述也说了这并不一定意味着元素是可见的,意味着不可见元素也能检测出来,但是你无法做其他操作。
visibility_of_element_located
检查元素是否存在于页面和可见。可见性意味着不仅显示元素但其高度和宽度也大于0。定位器-用于查找元素找到并可见WebElement后返回该WebElement
# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def visibility_located(self,value,*ele):
el = self.wait.until(EC.visibility_of_all_elements_located(ele), message='没有发现期望的元素')
el[0].send_keys(value)
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.visibility_located('qingan',By.NAME,'password')
此处值得说的是el[0],为什么是el[0],因为此处这样的写法返回后是个list列表,所以需要通过取值的方式将元素取出来。
url_to_be
检查当前url的期望值。url是预期的url,必须完全匹配。如果url匹配,则返回True,否则返回false
# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def url_be(self,url):
self.wait.until(EC.url_to_be(url))
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.url_be('http://shop.aircheng.com/simple/login')
判断url还有另外的几种方式,都大同小异,可以了解一下。
*「url_matches」 : 检查当前url的期望值。pattern是预期的模式,必须是完全匹配的如果url匹配,则返回True,否则返回false。 「url_contains」 : 检查当前url是否包含区分大小写的子字符串。url是所需url的片段,url匹配时返回True,否则返回False
*
「url_changes」 : 检查当前url的期望值。url是预期的url,不能完全匹配如果url不同,则返回True,否则返回false。
visibility_of
检查已知存在于页面的DOM是可见的。可见性意味着元素不仅仅是显示,但高度和宽度也大于0。元素是WebElement在WebElement可见时返回(相同的)WebElement
# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def visibility_(self,*ele):
el = self.wait.until(EC.visibility_of(self.fox.find_element(*ele)))
el.click()
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.visibility_(By.NAME, 'remember')
这里值得注意的是,visibility_of_element_located跟visibility_of很类似。与上面的写法不同EC.visibility_of里面我写的是定位,而非单纯的元素。这也是一个区别点。在后续的使用中注意一下。
*最后!说一下,上文中明明提起了True跟Flase但是没有看文中提起。
*
一、可以给定一个变量例如上文中的el,直接print(el)即可看到
二、是因为为True直接可以向下操作其他步骤了,没必要非要看到True。如果是False就会告诉你错误了。
边栏推荐
- 04 _ In simple terms index (I)
- 每日一博 - 微服务权限一二事
- Unified record of my code variable names
- 知网被立案调查;字节跳动成立抖音集团,或在港上市;钉钉被曝裁员30%;北京人均存款超20万元 |Q资讯
- 从0到1稳稳掌握大厂主流技术,年后涨薪不是必须的吗?
- MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size
- 英伟达终于开源GPU内核模块代码,网友:难以置信
- openGauss AI能力升级,打造全新的AI-Native数据库
- 码农必备SQL调优(下)
- Is it possible to use multiple indexes together in a query?
猜你喜欢

openGauss并行解码浅谈

Mysql(九)Your password has expired. To log in you must change it using a client that supports expired

MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size

【0006】title、关键字及页面描述

企业开发如何写出优雅的二级分类【美团小案例】

leetcode 120. 三角形最小路径和

19. insertion, deletion and pruning of binary search tree

openGauss数据库闪回功能验证

Learnopongl notes (IV) - Advanced OpenGL II

What is the future of software testing in 2022? Do you need to understand the code?
随机推荐
【创建型模式】建造者模式
做自媒体剪辑真的能挣钱吗?
openGauss并行解码浅谈
2022年软件测试的前景如何?需不需要懂代码?
openGauss数据库闪回功能验证
How to batch insert 100000 pieces of data
From 0 to 1, master the mainstream technology of large factories steadily. Isn't it necessary to increase salary after one year?
Oauth2的理解
线程实战入门【硬核慎入!】
06 _ Global lock and table lock: Why are there so many obstacles to adding a field to a table?
内存优化表MOT管理
Daily blog - wechat service permission 12 matters
The reason why it is easy to let the single chip computer program fly
前沿科技探究之AI功能:慢SQL发现
实时特征计算平台架构方法论和实践
07 _ 行锁功过:怎么减少行锁对性能的影响?
DB4AI: 数据库驱动AI
Installation and use of sonarqube
企业开发如何写出优雅的二级分类【美团小案例】
See from the minute, carve on the details: Exploration of SVG generated vector format website icon (favicon)