zoukankan      html  css  js  c++  java
  • selenium等待处理

    强制等待

    设置等待最简单的方法就是强制等待,其实就是time.sleep()方法,不管它什么情况,让程序暂停运行一定时间,时间过后继续运行;缺点时不智能,设置的时间太短,元素还没有加载出来,那照样会报错;设置的时间太长,则会浪费时间,不要小瞧每次几秒的时间,case多了,代码量大了,很多个几秒就会影响整体的运行速度了;所以尽量少用这个

    隐形等待

    driver.implicitly_wait(),隐性等待设置了一个时间,在一段时间内网页是否加载完成,如果完成了,就进行下一步;在设置的时间内没有加载完成,则会报超时加载;

    # -*- coding: utf-8 -*-  
    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()  
    driver.implicitly_wait(20) # 隐性等待,最长等30秒  
    driver.get('https://www.baidu.com')
    time.sleep(3)
    driver.quit()
    

     缺点也是不智能,因为随着ajax技术的广泛应用,页面的元素往往都可以时间局部加载,也就是在整个页面没有加载完的时候,可能我们需要的元素已经加载完成了,那就么有必要再等待整个页面的加载,执行进行下一步,而隐性等待满足不了这一点;

      另外一点,隐性等待的设置时全局性的,在开头设置过之后,整个的程序运行过程中都会有效,都会等待页面加载完成;不需要每次设置一遍;

    显性等待

    WebDriverWait(driver, 20, 0.5).until(expected_conditions.presence_of_element_located(locator)),selenium中的wait模块的WebDriverWait()方法,配合until或者until_not方法,再辅助以一些判断条件,就可以构成这样一个场景:每经过多少秒就查看一次locator的元素是否可见,如果可见就停止等待,如果不可见就继续等待直到超过规定的时间后,报超时异常;当然也可以判断某元素是否在规定时间内不可见等等的各种场景吧,需要根据你自己实际的场景选择判断条件;

    # -*- coding: utf-8 -*-  
    from selenium import webdriver  
    from selenium.webdriver.support.wait import WebDriverWait  
    from selenium.webdriver.support import expected_conditions as EC  
    from selenium.webdriver.common.by import By  
    driver = webdriver.Firefox()
    driver.get('https://huilansame.github.io')  
    WebDriverWait(driver,20,0.5).until(
        EC.presence_of_element_located((By.LINK_TEXT, 'CSDN')))  
    print driver.find_element_by_link_text('CSDN').get_attribute('href')
    driver.close()
    

    expected_conditions模块中提供了很多可以提供判断的条件:

    selenium.webdriver.support.expected_conditions(模块)  
    
    这两个条件类验证title,验证传入的参数title是否等于或包含于driver.title  
    
    title_is  
    
    title_contains  
    
    这两个人条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID, 'kw')  
    
    顾名思义,一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行  
    
    presence_of_element_located  
    
    presence_of_all_elements_located  
    
    这三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement  
    
    第一个和第三个其实质是一样的  
    
    visibility_of_element_located  
    
    invisibility_of_element_located  
    
    visibility_of  
    
    这两个人条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value  
    
    text_to_be_present_in_element  
    
    text_to_be_present_in_element_value  
    
    这个条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElement  
    
    frame_to_be_available_and_switch_to_it  
    
    这个条件判断是否有alert出现  
    
    alert_is_present  
    
    这个条件判断元素是否可点击,传入locator  
    
    element_to_be_clickable  
    
    这四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组  
    
    第三个传入WebElement对象以及状态,相等返回True,否则返回False  
    
    第四个传入locator以及状态,相等返回True,否则返回False  
    
    element_to_be_selected  
    
    element_located_to_be_selected  
    
    element_selection_state_to_be  
    
    element_located_selection_state_to_be  
    
    最后一个条件判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新了  
    
    staleness_of
    

    参考链接

  • 相关阅读:
    java中4种修饰符访问权限的区别
    Java中List的排序方法
    Hibernate事务
    @Component、@Service、@Constroller
    MySQL查看一个表的创建文本以及删除表某列的索引
    深入Session2
    Tomcat容器的Session管理
    深入Session
    使用spring mvc或者resteasy构建restful服务
    Spring MVC学习回顾
  • 原文地址:https://www.cnblogs.com/jokerBi/p/10938472.html
Copyright © 2011-2022 走看看