5 元素等待

元素等待

1 显示等待(了解)

概念:使WebDriver等待指定元素条件成立时继续执行,否则在达到最大时长时抛出超时异常(TimeoutException)

  • 在WebDriver中把显式等待的相关方法封装在WebDriverWait类中
  • 等待是判定条件成立时,那如何判断条件成立?相关判断的方法封装在expected_conditions类中

实现难点分析

1. 导包 等待类   from selenium.webdriver.support.wait import WebDriverWait
2. 导包 判断条件   from selenium.webdriver.support import expected_conditions as EC  (将expected_conditions 通过as关键字起个别名:EC)
3. WebDriverWait(driver, timeout, poll_frequency=0.5)
        1). driver:浏览器对象
        2). timeout:超时的时长,单位:秒
        3). poll_frequency:检测间隔时间,默认为0.5秒
4. 调用方法 until(method):直到..时
        1). method:调用EC.presence_of_element_located(element)
                    element:调用By类方法进行定位

代码实例

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
url = r'E:\测试\课件\Web自动化\Web自动化课件\02img\注册A.html'
driver = webdriver.Firefox()
driver.get(url)
element=WebDriverWait(driver,5).until(EC.presence_of_element_located((By.ID, 'userA')))
element.send_keys("admin")

2 隐式等待(掌握)

说明:等待元素加载指定的时长,超出抛出NoSuchElementException异常,实际工作中,一般都使用隐式等待;

显式与隐式区别:
    1. 作用域:显式等待为单个元素有效,隐式为全局元素
    2. 方法:显式等待方法封装在WebDriverWait类中,而隐式等待则直接通过浏览器实例化对象调用

隐式等待调用方法

方法:implicitly_wait(timeout)
      (timeout:为等待最大时长,单位:秒)

调用:driver.implicitly_wait(10)
      (driver:为浏览器实例化对象名称)
如果定位某一元素定位失败,那么就会触发隐式等待有效时长,如果在指定时长内加载完毕,则继续执行,否则抛出NoSuchElementException异常,如果元素在第一次就定位到则不会触发隐式等待时长

猜你喜欢

转载自www.cnblogs.com/shibojie/p/11726956.html