selenium自动化之显式等待和EC(expected_conditions)模块

很多人都有这种经历,selenium脚本当前运行没问题,过了一段时间再运行就报错了,然后过几天又好了。其中的原因估计60%的人都知道,是因为元素加载这块有问题。通常的解决方案就是加上sleep或者隐式等待(implicitly_wait),后面发现这种办法太浪费时间了,测试用例一旦过多就要抓狂了,并且还是不太稳定。

    其实,要想提升selenium脚本的稳定性和速度,显式等待结合EC(expected_conditions)模块是个非常不错的选择,下面是python语言的写法(人生苦短,我用python),大家可以进行二次封装。

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @创建时间: 2018/10/16 09:01
# @创建人  : Kevin
# @IDE    : PyCharm

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('url')

"""
   driver不用多说
   10表示最大的超时时间(s)
     0.1表示查询元素的频率
"""
'''判断title是否是一致,返回布尔值'''
WebDriverWait(driver,10,0.1).until(EC.title_is("title_text"))

'''判断title是否与包含预期值,返回布尔值'''
WebDriverWait(driver,10,0.1).until(EC.title_contains("title_text"))

'''判断某个元素是否被加到了dom树里,并不代表该元素一定可见,如果定位到就返回元素'''
WebDriverWait(driver,10,0.1).until(EC.presence_of_element_located((locator)))

'''判断某个元素是否被添加到了dom里并且可见,可见代表元素可显示且宽和高都大于0'''
WebDriverWait(driver,10,0.1).until(EC.visibility_of_element_located((locator)))

'''判断元素是否可见,如果可见就返回这个元素'''
WebDriverWait(driver,10,0.1).until(EC.visibility_of(driver.find_element(locator)))

'''判断是否至少有1个元素存在于dom树中,如果定位到就返回列表'''
WebDriverWait(driver,10,0.1).until(EC.presence_of_all_elements_located((locator)))

'''判断是否至少有一个元素在页面中可见,如果定位到就返回列表'''
WebDriverWait(driver,10,0.1).until(EC.visibility_of_any_elements_located((locator)))

'''判断指定的元素中是否包含了预期的字符串,返回布尔值'''
WebDriverWait(driver,10,0.1).until(EC.text_to_be_present_in_element((locator),'预期的text'))

'''判断指定元素的value属性值中是否包含了预期的字符串,返回布尔值(注意:只是value属性)'''
WebDriverWait(driver,10,0.1).until(EC.text_to_be_present_in_element_value((locator),'预期的text'))

'''判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False'''
WebDriverWait(driver,10,0.1).until(EC.frame_to_be_available_and_switch_to_it(locator))

'''判断某个元素在是否存在于dom或不可见,如果可见返回False,不可见返回这个元素'''
WebDriverWait(driver,10,0.1).until(EC.invisibility_of_element_located((locator)))

'''判断某个元素是否可见并且是可点击的,如果是的就返回这个元素,否则返回False'''
WebDriverWait(driver,10,0.1).until(EC.element_to_be_clickable((locator)))

'''等待某个元素从dom树中移除'''
WebDriverWait(driver,10,0.1).until(EC.staleness_of(driver.find_element(locator)))

'''判断某个元素是否被选中了,一般用在下拉列表'''
WebDriverWait(driver,10,0.1).until(EC.element_to_be_selected(driver.find_element(locator)))

'''判断某个元素的选中状态是否符合预期'''
WebDriverWait(driver,10,0.1).until(EC.element_selection_state_to_be(driver.find_element(locator),True))

'''判断某个元素的选中状态是否符合预期'''
WebDriverWait(driver,10,0.1).until(EC.element_located_selection_state_to_be((locator),True))

'''判断页面上是否存在alert,如果有就切换到alert并返回alert的内容'''
accept = WebDriverWait(driver,10,0.1).until(EC.alert_is_present())
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值