一、显示等待

所谓显示等待,是针对某一个特定的元素设置等待时间,如果在规定的时间内找到了该元素,就执行相关的操作,如果在规定的时间内没有找到该元素,在抛出异常

PS:注意显示等待和隐身等待的区别,隐身等待是对页面中的所有元素设置加载时间。

二、WebDriverWait类

在selenium框架中显示等待是通过WebDriverWait来实现的

WebDriverWait所在位置:在support包的wait.py文件中,selenium\webdriver\support\wait.py

构成函数:

class WebDriverWait(object):
def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
pass

构造函数参数解读:

driver:webdriver实例,可以是Ie, Firefox, Chrome or Remote
timeout:查找特定元素时的最大超时时间,单位seconds
poll_frequency:关键字参数,查找元素的频率,即每隔多少时间回调一次method,默认为 0.5 second
ignored_exceptions:关键字参数,在回调method过程中忽略哪些异常,默认为NoSuchElementException,如果有需要忽略多个异常,可以以iterable的形式传入参数,如tuple

实例方法:
def until(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value is not False.""" def until_not(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value is False."""

方法解读:

until:循环调用method的方法,直到method返回true()非false,返回结果:method方法返回的结果
until_not:循环调用method的方法,直到method返回false
在这两个方法中,参数methon是必传参数,且传入的是一个functhon对象(注意是函数对象,不带括号的),该method对象的参数是driver
def until(self, method, message=''):
screen = None
stacktrace = None # 设置超时时间
end_time = time.time() + self._timeout
while True: # 循环调用method方法
try:
# 执行传入的methon方法,且参数为driver
value = method(self._driver)
if value: # 如果method方法执行的结果为非false,则停止循环
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 TimeoutException(message, screen, stacktrace)

举个例子:

from selenium.webdriver.support.ui import WebDriverWait # 导入WebDriverWait
# method:参数为driver的lambda匿名函数
# 功能:找到id为someId的元素
# 返回值:x.find_element_by_id("someId")的WebElement对象
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId"))

总结:

until / until_not方法

参数:method对象(注意是对象,而非调用方法),可以传入lambda匿名函数

method方法的参数为driver

selenium之python源码解读-WebDriverWait的更多相关文章

  1. selenium之python源码解读-expected_conditions

    一.expected_conditions 之前在 selenium之python源码解读-WebDriverWait 中说到,until方法中method参数,需要传入一个function对象,如果 ...

  2. selenium之python源码解读-webdriver继承关系

    一.webdriver继承关系 在selenium中,无论是常用的Firefox Driver 还是Chrome Driver和Ie Drive,他们都继承至selenium\webdriver\re ...

  3. 如何判断一个Http Message的结束——python源码解读

    HTTP/1.1 默认的连接方式是长连接,不能通过简单的TCP连接关闭判断HttpMessage的结束. 以下是几种判断HttpMessage结束的方式: 1.      HTTP协议约定status ...

  4. python 源码解读2

    http://www.jianshu.com/users/4d4a2f26740b/latest_articles http://blog.csdn.net/ssjhust123/article/ca ...

  5. Apache Beam WordCount编程实战及源码解读

    概述:Apache Beam WordCount编程实战及源码解读,并通过intellij IDEA和terminal两种方式调试运行WordCount程序,Apache Beam对大数据的批处理和流 ...

  6. 基于Docker的TensorFlow机器学习框架搭建和实例源码解读

    概述:基于Docker的TensorFlow机器学习框架搭建和实例源码解读,TensorFlow作为最火热的机器学习框架之一,Docker是的容器,可以很好的结合起来,为机器学习或者科研人员提供便捷的 ...

  7. Spark学习之路 (十六)SparkCore的源码解读(二)spark-submit提交脚本

    一.概述 上一篇主要是介绍了spark启动的一些脚本,这篇主要分析一下Spark源码中提交任务脚本的处理逻辑,从spark-submit一步步深入进去看看任务提交的整体流程,首先看一下整体的流程概要图 ...

  8. ansible源码解读

    Ansible源码获取 Ansible Github:https://github.com/ansible Ansible目录结构 $ tree -L 2 ansible-2.0.0.0 ansibl ...

  9. DRF(1) - REST、DRF(View源码解读、APIView源码解读)

    一.REST 1.什么是编程? 数据结构和算法的结合. 2.什么是REST? 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下: /books/ /get_all_books/ 访问所 ...

随机推荐

  1. LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

    637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...

  2. 035 Android 广播(BroadCastReceiver)

    1.介绍 2.实现方法 3.注册广播 (1)静态广播 在AndroidManifest.xml文件中注册广播 <intent-filter>为过滤器 <receiver androi ...

  3. 原生js 实现better-scroll效果,饿了么菜单内容联动,即粘即用

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. @Scheduled注解各参数详解

    @Scheduled注解各参数详解 @Scheduled注解的使用可以参考这个:https://www.cnblogs.com/mengw/p/11564338.html 参数详解 1. cron 该 ...

  5. Spyder中报错: Check failed: PyBfloat16_Type.tp_base != nullptr

    报错问题: 问题1:tensorflow/python/lib/core/bfloat16.cc:675] Check failed: PyBfloat16_Type.tp_base != nullp ...

  6. Python虚拟环境virtualenv的安装与使用详解(转)

    virtualenv参考:https://www.jb51.net/article/114933.htm virtualenvwrapper参考:https://www.jianshu.com/p/7 ...

  7. jquery.marquee

    http://aamirafridi.com/jquery/jquery-marquee-plugin#examples <script src="/plugins/marquee/j ...

  8. em...刚打完一点cf。。 有点子感悟

    首先,下笔一定要读清楚题目. 情况多考虑一下. 这几次的模拟赛,分类思想很重要,往往一大坨东西扔给你,你不去尝试分类的话就很难整理清楚.

  9. Powershell学习笔记:(一)、初识Powershell

    什么是Powershell? MSDN上的说明是:PowerShell 是构建于 .NET 上基于任务的命令行 shell 和脚本语言. PowerShell 可帮助系统管理员和高级用户快速自动执行用 ...

  10. VS 2015 .net UI界面报错总结

    一.提示錯誤 解決方法: 右击解决方案点击properties Window Ctrl+W ,P 将Mnaged Pipeline Mode 从Integrated更改为Classic 二.提示錯誤 ...