zoukankan      html  css  js  c++  java
  • Selenium:截图显示等待

    一、显示等待(有条件等待)

    常见问题:

    1. 定位明明是对的,为什么运行代码没找到定位。
    2. 定位明明是对的,找到定位了,文本信息为什么取到是空的?

    分析原因:

    1. 没有处理frame
    2. 页面渲染速度比自动化测试的代码慢,页面还没渲染出来就定位了
    3. 异步请求,后端还未返回给前端,自动化测试代码就去获取页面文本数据

    一些同学的解决解决方案:

    1. 加上time.sleep()
    2. 设置全局隐式等待,dr.implicitly_wait()

    分析解决方案:

    1. 解决方案一,我们不能够确定准确的强制等待时间(代码休眠时间),导致自动化代码不稳定性大大增加,尤其是网络波动的影响,会导致页面加载速度变慢
    2. 全局等待虽可以解决一部分定位问题,例如需要定位的标签属性显现,但是如果像异步请求,需等待带有文本信息的相应,并渲染在页面上,不能确定在自动化代码执行获取文本信息的时候,页面已经渲染好所需要的文本信息

    什么是显示等待?

    • 条件满足后,不再等待

    二、显示等待相关类

    Wait.py

    class WebDriverWait(object):
        def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
            """构造函数,需要一个WebDriver实例并以秒为单位超时
               :参数:
                - driver - 传入webdriver实例对象
                - timeout - 超时时间,单位 秒
                - poll_frequency - 程序休眠时间,默认0.5秒
                - ignored_exceptions - 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,
                则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。
    
               使用例子:
                from selenium.webdriver.support.ui import WebDriverWait \n
                element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
                is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
                            until_not(lambda x: x.find_element_by_id("someId").is_displayed())
            """
        
        def until(self, method, message=''):
            """在规定的超时时间内,调用传入的方法,直到返回的结果为真,否则抛出超时的异常"""
            screen = None
            stacktrace = None
    
            end_time = time.time() + self._timeout
            while True:
                try:
                    value = method(self._driver)
                    if value:
                        return value
                except self._ignored_exceptions as exc:
                    screen = getattr(exc, 'screen', None)
                    stacktrace = getattr(exc, 'stacktrace', None)
                time.sleep(self._poll)
                if time.time() > end_time:
                    break
            raise TimeoutException(message, screen, stacktrace)
        
        def until_not(self, method, message=''):
            """与unitl方法相反,不多解释,项目中基本用不到"""
            end_time = time.time() + self._timeout
            while True:
                try:
                    value = method(self._driver)
                    if not value:
                        return value
                except self._ignored_exceptions:
                    return True
                time.sleep(self._poll)
                if time.time() > end_time:
                    break
            raise TimeoutException(message)
    

    expected_conditions.py

    常用

    每个类的具体用法查看源码,写的非常清楚
    /Python/Python36/Lib/site-packages/selenium/webdriver/support

    # 判断页面title等于预期值
    title_is 
    
    # 判断页面title包含预期字符串
    title_contains 
    
    # 判断当前url等于预期url
    url_to_be
    
    # 判断当前url包含预期字符串
    url_matches  # 正则匹配 re
    url_contains # 包含 in
    
    # 判断当前url不等于预期url
    url_changes
    
    # 判断元素显现并定位成功
    visibility_of_element_located
    
    # 判断元素显现(不一定能定位,能定位的元素大小需大于0)
    visibility_of
    
    # 判断获取文本信息包含预期的文本信息
    text_to_be_present_in_element
    
    # 判断获取属性值的信息包含预期的文本信息
    text_to_be_present_in_element_value
    
    # 判断iframe可以切换,如果为真则切换,反之返回False
    frame_to_be_available_and_switch_to_it
    
    # 判断元素可以点击
    element_to_be_clickable
    
    # 判断元素被选中
    element_to_be_selected
    element_located_to_be_selected
    
    # 判断窗口数量
    number_of_windows_to_be
    
    # 判断新窗口打开
    number_of_windows_to_be
    
    # 警告框显现
    alert_is_present
    

    不常用

    presence_of_element_located
    presence_of_all_elements_located
    visibility_of_any_elements_located
    visibility_of_all_elements_located
    invisibility_of_element_located
    invisibility_of_element(invisibility_of_element_located)
    staleness_of
    element_selection_state_to_be
    element_located_selection_state_to_be
    

    三、显示等待使用

    demo.py

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    driver = webdriver.Chrome()
    driver.get('https://www.baidu.com')
    
    # 输入框定位器
    locator_of_search = (By.CSS_SELECTOR,'#kw')
    
    # 元素显现并定位成功后,不再等待,反则抛出超时异常
    WebDriverWait(driver=driver,timeout=10,poll_frequency=0.5).until(EC.visibility_of_element_located(locator_of_search))
    driver.find_element(*locator_of_search).send_keys('测试显示等待')
    
  • 相关阅读:
    FZOJ2115+月赛+多项式
    Statistical Data Mining Tutorials [转]
    码农何去何从
    关于InnoDB索引长度限制的tips
    虚拟化、云计算、开放源代码及其他
    互联网开放平台应用综述
    2012.09月面试五十题
    linux运维常用命令
    Linux 性能测试与分析转
    "Principles of Computer Systems Design"
  • 原文地址:https://www.cnblogs.com/gqv2009/p/12218227.html
Copyright © 2011-2022 走看看