zoukankan      html  css  js  c++  java
  • webdriver中的等待-WebDriverWait()

    1、强制等待:sleep()  

    from time import sleep
    sleep(3) #等待3秒

    ****官方不推荐这样的方法,使用太多的sleep会影响脚本运行速度

    2、隐式等待:implicitly_wait() 

    driver.implicitly_wait(10) #隐式等待10秒

    由webdriver提供的方法,一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用,它不针对某一个元素,是全局元素等待,即在定位元素时,需要等待页面全部元素加载完成,才会执行下一个语句。如果超出了设置时间的则抛出异常。
    缺点:当页面某些js无法加载,但是想找的元素已经出来了,它还是会继续等待,直到页面加载完成(浏览器标签左上角圈圈不再转),才会执行下一句。某些情况下会影响脚本执行速度。
    3、显示等待:WebDriverWait()

    from selenium.webdriver.support.ui import WebDriverWait
    WebDriverWait(self.driver, 5) # 等待5秒

    具体样式:

    WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)
    • driver:浏览器驱动
    • timeout:最长超时时间,默认以秒为单位
    • poll_frequency:检测的间隔步长,默认为0.5s
    • ignored_exceptions:超时后的抛出的异常信息,默认抛出NoSuchElementExeception异常。

    通常会与until()或者until_not()方法结合使用:

    WebDriverWait(driver,10).until(method,message="")
    调用该方法提供的驱动程序作为参数,直到返回值为True

    WebDriverWait(driver,10).until_not(method,message="")
    调用该方法提供的驱动程序作为参数,直到返回值为False

    在设置时间(10s)内,等待后面的条件发生。如果超过设置时间未发生,则抛出异常。在等待期间,每隔一定时间(默认0.5秒),调用until或until_not里的方法,直到它返回True或False.

    WebDriverWait与expected_conditions结合使用

    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    wait = WebDriverWait(driver,10,0.5)
    element =waite.until(EC.presence_of_element_located((By.ID,"kw")),message="")
    # 此处注意,如果省略message=“”,则By.ID外面是两层()

    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 都是’wp’,那么只要有 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

    场景运用

      这里是在笔者实际使用的情况:

    waite = WebDriverWait(self.driver, 5)
    element = waite.until(EC.title_is("XX首页"), '页面标题显示异常!') # 如果页面的标题显示不是“XX首页”,则会抛出异常“页面标题显示异常”
    waite = WebDriverWait(self.driver, 4)
    ele = waite.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#厂家$Menu > li:nth-child(4)')), '未找到对应元素')
    ele.click()

    也可以这样:

    WebDriverWait(self.driver, 1).until(EC.presence_of_element_located((By.ID, 'name')), '未找到姓名').send_keys(name)
    WebDriverWait(self.driver, 1).until(EC.presence_of_element_located((By.ID, 'certNo')), '未找到身份证').send_keys(idcard)

    具体的使用,得看项目的情况而论。

  • 相关阅读:
    jQuery事件委托
    jQuery-事件面试题
    jQuery事件处理
    文档—CUD
    jQuery练习
    jQuery-筛选
    5. Longest Palindromic Substring
    340. Longest Substring with At Most K Distinct Characters
    159. Longest Substring with At Most Two Distinct Characters
    438. Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/ruichow/p/12424913.html
Copyright © 2011-2022 走看看