• Selenium自动化测试-设置元素等待


    selenium中有三种时间等待

    • 强制等待:sleep

    • 隐式等待:implicitly_wait

    • 显示等待:WebDriverWait

    1.sleep

    让程序暂停运行一定时间,等待时间到达后继续运行。

    使用sleep,需先导入time模块,import time, 然后使用time.sleep()来让程序等待多久。

     
    
    1. from selenium import webdriver

    2. import time

    3. driver = webdriver.Chrome()

    4. driver.get('https://www.baidu.com/')

    5. # 等待3s

    6. time.sleep(3)

    7. # 点击新闻链接

    8. driver.find_element_by_link_text("新闻").click()

    3.  WebDriverWait

    等待某个条件成立时继续执行,否则在达到最大时长时抛出超时异常TimeoutException。

    WebDriverWait一般和until()和until_not()配合使用:

    • until() 当某元素出现或什么条件成立则继续执行

    • until_not 当某元素消失或什么条件不成立则继续执

    WebDriverWait(driver, timeout, poll_frequency=0.5,

    ignored_exceptions=None)

    • driver: 传入WebDriver实例;
    • timeout:指最大超时时间,默认单位为秒;
    • poll_frequency:调用until或until_not方法,每隔一定时间不断尝试是否能找到页面元素,默认间隔是0.5s,可自行调整间隔时间。
    • ignored_exceptions:超时后的异常信息,默认情况下NoSuchElementException 异常。

    使用WebDriverWait,需要先导入WebDriverWait模块。

    from selenium.webdriver.support.ui import WebDriverWait

    我们使用WebDriverWait方式来定位百度页面的新闻链接,

    代码如下:

     
    
    1. from selenium import webdriver

    2. from selenium.webdriver.support.ui import WebDriverWait

    3. driver = webdriver.Chrome()

    4. driver.get('https://www.baidu.com/')

    5. # 设置显式等待,超时时长最大为 5s,每隔0.5s查找元素一次

    6. element = WebDriverWait(driver,5).until(

    7. lambda x: x.find_element_by_link_text('新闻'))

    8. element.click()

    注意:until或until_not中的method参数一定要是可以调用的对象,即这个对象一定有 __call__方法,否则会抛出异常。

    例如:

     
    
    1. from selenium import webdriver

    2. from selenium.webdriver.support.ui import WebDriverWait

    3. driver = webdriver.Chrome()

    4. driver.get('https://www.baidu.com/')

    5. # 设置显式超时时长最大为5s,每隔0.5s查到元素一次

    6. element = WebDriverWait(driver,5).until(

    7. driver.find_element_by_link_text('新闻'))

    8. element.click()

    运行的结果是报错的:
     
    
    1. Traceback (most recent call last):

    2. File "C:/Users/96984/PycharmProjects/vivi_python/selenium_vivi/radio.py", line 7, in

    3. element = WebDriverWait(driver,5).until(driver.find_element_by_link_text('新闻'))

    4. File "C:\Users\96984\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until

    5. value = method(self._driver)

    6. TypeError: 'WebElement' object is not callable

    所以我们可以用selenium提供的expected_conditions,提供一些场景的判断,或者用自己封装的方法都可以。

    使用expected_conditions,需先导入。

    from selenium.webdriver.support import expected_conditions as EC

    以下是常用的expected_conditions方法:

    方法说明
    title_is

    判断当前页面的 title 是否完全等于(==)

    预期字符串,返回布尔值

    title_contains

    判断当前页面的 title 是否包含预期字符串,返回布尔值

    presence_of_element_located

    判断某个元素是否被加到了 dom 树里,并不代表该元素一定可见

    visibility_of_element_located

    判断元素是否可见(可见代表元素非隐藏,并且元素宽和高都不等于 0)

    visibility_of

    同上一方法,只是上一方法参数为locator,这个方法参数是 定位后的元素

    presence_of_all_elements_located

    判断是否至少有 1 个元素存在于 dom 树中。举例:如果页面上有 n 个元素的 class 都是’qw’,那么只要有 1 个元素存在,这个方法就返回 True

    text_to_be_present_in_element

    判断某个元素中的 text 是否包含了预期的字符串

    text_to_be_present_in_element_value

    判断某个元素中的 value 属性是否包含了预期的字符串

    frame_to_be_available_and_switch_to_it

    判断该 frame 是否可以 switch进去,如果可以的话,返回 True 并且 switch 进去,否则返回 False

    invisibility_of_element_located判断某个元素中是否不存在于dom树或不可见
    element_to_be_clickable判断某个元素中是否可见并且可点击
    staleness_of

    等某个元素从 dom 树中移除,注意,这个方法也是返回 True或 False

    element_to_be_selected判断某个元素是否被选中了,一般用在下拉列表
    element_selection_state_to_be判断某个元素的选中状态是否符合预期
    element_located_selection_state_to_be

    跟上面的方法作用一样,只是上面的方法传入定位到的 element,而这个方法传入 locator

    alert_is_present判断页面上是否存在 alert

    我们以presence_of_element_located这个方法为例,看下WebDriverWait怎么和expected_conditions配合使用。

     
    
    1. from selenium import webdriver

    2. from selenium.webdriver.support.ui import WebDriverWait

    3. from selenium.webdriver.support import expected_conditions as EC

    4. driver = webdriver.Chrome()

    5. driver.get('https://www.baidu.com/')

    6. # 设置显式等待,超时时长最大为5s,每隔0.5s查找元素一次

    7. element = WebDriverWait(driver,5).until(

    8. EC.presence_of_element_located(('id','kw')))

    9. element.send_keys('vivi')

    最后总结下三种元素等待的优缺点:

    元素等待方式

    优点

    缺点

    ime

    使用简单,在程序调试时使用

    浪费不必要的等待时间,影响用例执行效率

    implicitly_wait

    一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用

    程序等待整个页面加载完成,才会下一步操作,不够灵活

    WebDriverWait

    条件成立,就会下一步操作,节省等待时间,提高用例执行效率

    使用相对复杂,学习成本相对较大

     

    总结:

    感谢每一个认真阅读我文章的人!!!

    作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

    软件测试面试文档

    我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

     

              视频文档获取方式:
    这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方进群即可自行领取。

  • 相关阅读:
    RabbitMQ的安装和配置
    Linux操作系统 实现一个进程管理子系统
    版本管理的使用
    Ubuntu安装中文man手册
    小学期,第三场-下午:WEB_xxe
    【JAVA程序设计】(C00090) 基于SSM+Vue的宠物医院预约挂号平台
    【数据结构】单链表OJ题(二)
    Qt应用程序打包工具安装
    【华为ICT大赛】华为云激活设备的方法以及数据上下行
    【STL编程】【竞赛常用】【part 3】
  • 原文地址:https://blog.csdn.net/2301_77645834/article/details/136714894