web自动化测试页面等待

本文介绍了WebDriver中的显式等待、隐式等待和强制等待。显式等待使用WebDriverWait,通过设置超时时间和判断条件来确保元素可用。隐式等待在查找元素时全局生效,但不建议与显式等待混用。强制等待通过time.sleep实现,常用于页面加载慢的情况。文章还提及了expected_conditions模块中的各种判定条件。
摘要由CSDN通过智能技术生成

 

Waits 等待


 

WebDriver因为是异步触发元素或事件,有些时候脚本可能是间接性的,也有可能是断断续续的;又加浏览器和WebDriver脚本之间的存在竞争条件问题,因此我们需要引入等待包,让脚本运行不会出现阻塞或失败

 

使用等待时,我们需要引入等待包:

ui和wait里面都有WebDriverWait类,目前两种引包使用都没问题的;区别的话就是:ui模块里面引入的类就是wait下的WebDriverWait类 


from selenium.webdriver.support.ui import WebDriverWait


from selenium.webdriver.support.wait import WebDriverWait


三大等待之:显式等待

显式等待部分源码简介:


class WebDriverWait(object):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, 
        self._driver = driver
        self._timeout = timeout
        self._poll = poll_frequency
        # avoid the divide by zero
        if self._poll == 0:
            self._poll = POLL_FREQUENCY
        exceptions = list(IGNORED_EXCEPTIONS)
        if ignored_exceptions is not None:
            try:
                exceptions.extend(iter(ignored_exceptions))
            except TypeError:  # ignored_exceptions is not iterable
                exceptions.append(ignored_exceptions)
        self._ignored_exceptions = tuple(exceptions)

    ...... # 省略的源码

    def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        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 TimeoutExcept
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值