selenium-webdriver中的显式等待与隐式等待

在selenium-webdriver中等待的方式简单可以概括为三种:
1 导入time包,调用time.sleep()的方法传入时间,这种方式也叫强制等待,固定死等一个时间

2 隐式等待,直接调用implicitly_wait()方法,传入等待时间,比如implicitly_wait(10),就代表最长等待10秒

3 显式等待,导入WebDriverWait和expected_conditions包,调用until()者until_not()方法

下面重点介绍后两种方式
一. 隐式等待: 表示在自动化实施过程中,为查找页面元素或者执行命令设置一个最长等待时间,如果在规定时间内也没元素被找到或命令被执行完成,则执行下一步,否则继续等待直到设置的最长等待时间截止

下面来看段代码:

from selenium import webdriver
import unittest, time
from selenium.common.exceptions import NoSuchElementException, TimeoutException
import traceback


class VisitSogouByChrome(unittest.TestCase):
def setUp(self):
    # 启动Chrome浏览器
    self.driver = webdriver.Chrome(executable_path="D:\\chromedriver")

def test_implictWait(self):
    url = 'https://www.baidu.com/'
    self.driver.get(url)
    # 设置隐式等待时间为10秒
    self.driver.implicitly_wait(10)
    try:
        search = self.driver.find_element_by_id("kw")
        search.send_keys("毒")
        click = self.driver.find_element_by_id("su")
        click.click()
    except (NoSuchElementException, TimeoutException):
        traceback.print_exc()

def tearDown(self):
    # 退出谷歌浏览器
    self.driver.quit()

这段代码基本就是完成打开百度搜索页,搜索毒的操作。可以看到隐式等待就是调用implicitly_wait()的方法就可以了。

隐式等待的好处是不用像强制等待(time.sleep(n))的方法一样死等固定时间n秒,可以在一定程度上提升测试用例的执行效率。但是这种方法也有一定弊端,就是程序将会一直等待到整个页面加载完成,也就是说浏览器窗口标签栏不再出现小圈圈,才会继续执行下一步,比如某些时候页面元素已经加再好了,但是某个js文件等待资源慢了点,此时程序仍然会等待页面全部加载完成才会执行下一步,这样加长了测试用例的执行时间。

注意:隐式等待时间只需要设置一次,然后他将在driver整个生命周期都起作用

加入我们,642830685,群。领取最新软件测试大厂面试资料和Python自动化、接口、框架搭建学习资料!技术大牛解惑答疑,同行一起交流

二. 显式等待: 通过selenium.webdriver.suppert.ui模块提供的WebDriverWait类,再结合该类的until()和until_not()的方法,并自定义好等待时间,根据判断条件进行灵活等待。 显式等待比隐式等待更节约执行时间,因此更推荐使用显式等待方式判断页面元素是否存在

WebDriverWait类解析:WebDriverWait类构造方法:

init(self, driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

参数解释:

driver:WebDriver实例对象(IE,Firefox, Chrome等)

timeout: 最长等待时间,单位微妙

poll_frequency: 调用评率,也就是timeout时间段内,每隔poll_frequency时间执行一次判断条件,默认0.5s

ignored_exceptions: 执行过程中忽略的异常对象,默认只忽略TimeoutException异常类

WebDriverWait类提供方法:
(1)until(method, message=’’)

在规定时间内,每隔一段时间调用一下method方法,至到期返回值不为False,如果超时抛出带有message的TimeoutException异常信息

(1)until_not(method, message=’’)

与until()方法相反,表示在规定时间内,每隔一段时间调用一下method方法,至到期返回值为False,如果超时抛出带有message的TimeoutException异常信息

下面这段代码是利用显式等待判断元素是否存在

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
def is_element_exsist2(driver, locator):
'''
结合WebDriverWait和expected_conditions判断元素是否存在,
每间隔1秒判断一次,30s超时,存在返回True,不存返回False
:param locator: locator为元组类型,如("id", "kw")
:return: bool值,True or False
'''
try:
    WebDriverWait(driver, 30, 1).until(EC.presence_of_element_located(locator))
    return True
except:
    return False
if __name__ == '__main__':
    loc1 = ("id", "kw")  # 元素1
    print(is_element_exsist2(driver, loc1))

显式等待期望场景
1、presence_of_element_located(locator)判断某个元素是否出现在DOM中,不一定可见,只要存在返回该页面元素对象使用方法: wait.until(EC.presence_of_element_located((By.ID, “kw”)))
2、alert_is_present()判断页面是否出现alert框,显示等待中使用方法:
wait = WebDriverWait(driver, 10)# 打印一下弹框信息
wait.until(EC.alert_is_present()).text
3、element_to_be_clickable(locator):判断某元素是否可见并能点击,使用方法: wait.until(EC.element_to_be_clickable((By.ID, “kw”)))
4、element_to_be_selected(locator):期望某个元素处于选中状态,参数为一个WebDrover实例对象,使用方法:wait.until(EC.element_to_be_selected((By.ID, “kw”)))
5、title_is(title_text):判断页面title内容是否与传入的title_text内容完全匹配,是返回Ture,否返回False,使用方法:wait.until(EC.title_is(u"百度一下"))

猜你喜欢

转载自blog.csdn.net/Asaasa1/article/details/107560987