参数说明:
method 在等待时间内调用的方法或者函数,该方法或函数需要有返回值,并且只接收一个参数driver。
message 超时时抛出TimeoutException,将message传入异常显示出来
WebDriverWait(driver, timeout).until_not(method, message=‘’)
于上面的until() 相反,until_not 中的method函数或者实例__call__()方法返回False结束,否则抛出异常。
until方法使用的method 的函数或者类__call()__方法详解:
函数我们一般采用匿名函数lambda 。
lambda driver:driver.find_element(<定位元素>) # 当定位的元素时为True,无元素时为False。如示例1、2:
WebDriverWait示例1:
WebDriverWait(driver,5).until(lambda driver:driver.find_element_by_id(‘query’))
5秒内等待元素(id=‘query’)出现,lambda driver:driver.find_element_by_id(‘query’) 为一个匿名函数,只有一个driver参数,返回的是查找的元素对象。
WebDriverWait示例2:
WebDriverWait(driver, 5).until_not(lambda driver:driver.find_element_by_name(‘query’))
5秒内等待元素消失,同示例1 until_not 要求无元素返回即元素不存在于该页面。
定义类中的__call()__方法。
class wait_element(object):
def init(self, locator):
self.locator = locator
def call(self, driver):
return driver.find_element(self.locator)
WebDriverWait(driver, 5).until(wait_element((By.ID, ‘query’))) # 等待元素出现
WebDriverWait(driver, 5).until_not(wait_element((By.ID, ‘query’))) # 等待元素消失
wait_element类中__init__()方法接收需要定位的元素,__call__()方法中只能有唯一变量driver,并且返回元素对象。
这样做是是不是很麻烦,其实selenium提供的一个库进行操作,expected_conditions库。引入位置
from selenium.webdriver.support import expected_conditions as ec,它囊括了我们需要使用等待的所有情况。
四、expected_conditions 类库
from selenium.webdriver.support import expected_conditions as ec # 引入包
下面示例都是以搜狗搜索首页为例。
方法中参数说明 locator=(By.ID, ‘id’) 表示使用By方法定位元素的元组,element表示获取的webElement元素对象。
ec.title_is(‘title’) 判断页面标题等于title
ec.title_contains(‘title’) 判断页面标题包含title
WebDriverWait(driver, 10).until(ec.title_is(‘搜狗搜索引擎 - 上网从搜狗开始’)) # 等待title 于参数相等
WebDriverWait(driver, 10).until(ec.title_contains(‘搜狗搜索引擎’)) # 等待title 包含 参数的内容
ec.presence_of_element_located(locator) 等待locator元素是否出现
ec.presence_of_all_elements_located(locator) 等待所有locator元素是否出现
WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.ID, ‘query’)))
WebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.ID, ‘query’)))
ec.visibility_of_element_located(locator) 等待locator元素可见
ec.invisibility_of_element_located(locator) 等待locator元素隐藏
ec.visibility_of(element) 等待element元素可见
WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.ID, ‘‘stb’’))) # 等待元素可见
WebDriverWait(driver, 10).until(ec.invisibility_of_element_located((By.ID, ‘‘stb’’))) # 等待元素隐藏
WebDriverWait(driver, 10).until(ec.visibility_of(driver.find_element_by_link_text(‘高级搜索’))) # 等待元素可见
ec.text_to_be_present_in_element(locator,text) 等待locator的元素中包含text文本
ec.text_to_be_present_in_element_value(locator,value) 等待locator元素的value属性为value
WebDriverWait(driver, 10).until(ec.text_to_be_present_in_element((By.ID, ‘erwx’), ‘搜索APP’)) # 等待元素中包含搜索APP文本
WebDriverWait(driver, 10).until(ec.text_to_be_present_in_element_value((By.ID, ‘query’),‘selenium’)) # 等待元素的值为 selenium,一般用于输入框
ec.frame_to_be_available_and_switch_to_it(locator) 等待frame可切入
WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it((By.ID, ‘frame’)))
WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it(‘frameid’))
ec.alert_is_present() 等待alert弹出窗口出现
WebDriverWait(driver, 10).until(ec.alert_is_present())
ec.element_to_be_clickable(locator) 等待locator元素可点击
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.ID, ‘kw’)))
等待元素被选中,一般用于复选框,单选框
ec.element_to_be_selected(element) 等待element元素是被选中
ec.element_located_to_be_selected(locator) 等待locator元素是被选中ec.element_selection_state_to_be(element, is_selected) 等待element元素的值被选中为is_selected(布尔值)
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数软件测试工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上软件测试开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注软件测试)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注软件测试)**
[外链图片转存中…(img-eD7P9l7I-1712699995031)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!