一、expected_conditions

之前在 selenium之python源码解读-WebDriverWait 中说到,until方法中method参数,需要传入一个function对象,如果每次都自定义或者使用lambda函数,显得比较麻烦。

其实在expected_conditions中,Selenium提供了一些常用的元素查找的条件类

在selenium\webdriver\support\expected_conditions.py中定义的所有类如下:

class title_contains(object):
class presence_of_element_located(object):
class visibility_of_element_located(object):
class visibility_of(object):
class presence_of_all_elements_located(object):
class visibility_of_any_elements_located(object):
class visibility_of_all_elements_located(object):
class text_to_be_present_in_element(object):
class text_to_be_present_in_element_value(object):
class frame_to_be_available_and_switch_to_it(object):
class invisibility_of_element_located(object):
class element_to_be_clickable(object):
class staleness_of(object):
class element_to_be_selected(object):
class element_located_to_be_selected(object):
class element_selection_state_to_be(object):
class element_located_selection_state_to_be(object):
class number_of_windows_to_be(object):
class new_window_is_opened(object):
class alert_is_present(object):

在上面定义的类中都实现了 __call__(self, driver)方法,这就意味着该类的对象是一个可调用的对象

如以visibility_of_element_located类为例:

实例化一个对象:visibility_of_element_located(locator)

在对象后加():调用对象,即调用类的 __call__方法,visibility_of_element_located(locator)(driver)

二、类中的 __init__  和__call__方法传参总结

1、传入locator,和driver参数

def __init__(self, locator):
  self.locator = locator def __call__(self, driver):
  return _find_element(driver, self.locator)

而locator,需要传入一个iterateble对象,如tuple,(by.ID,"id")

def _find_element(driver, by):
"""Looks up an element. Logs and re-raises ``WebDriverException``
if thrown."""
try:
return driver.find_element(*by)
except NoSuchElementException as e:
raise e
except WebDriverException as e:
raise e

2、传入element,和ignored参数

def __init__(self, element):
self.element = element def __call__(self, ignored):
return _element_if_visible(self.element)

该类型的传参有4个

class visibility_of(object)

class staleness_of(object)

class element_located_to_be_selected(object)

class element_selection_state_to_be(object)
lement_located_to_be_selected(object)和element_selection_state_to_be(object)实现的功能是一样的

3、其他还有title、window、handler、alert此处不再一一例举,具体查看源码

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

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

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

  2. selenium之python源码解读-WebDriverWait

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

  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. [转帖]ORM框架的前世今生

    ORM框架的前世今生 https://www.cnblogs.com/7tiny/p/9551754.html 目录 一.ORM简介二.ORM的工作原理三.ORM的优缺点四.常见的ORM框架 一.OR ...

  2. IdentityServer4 学习三

    ClientCredentials客户端类型实现 客户端应用向IdentityServer请求AccessToken,IdentityServer验证通过把AccessToken返回给客户端应用,客户 ...

  3. Python Threading 线程/互斥锁/死锁/GIL锁

    导入线程包 import threading 准备函数线程,传参数 t1 = threading.Thread(target=func,args=(args,)) 类继承线程,创建线程对象 class ...

  4. 【转】使用ASP.NET Web API构建Restful API

    https://blog.csdn.net/mzl87/article/details/84947623 要点:使用DTO将内部实体与传输实体分离,利用Automapper实现两者的自动映射

  5. POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20309   Accepted:  ...

  6. JMeter-03-元件的作用域与执行顺序

    JMeter元件的作用域与执行顺序 元件的作用域 先来讨论一下元件有作用域.<JMeter基础元件介绍>一节中,我们介绍了8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样 ...

  7. .net mvc 迁移到 .netcore

    迁移的时候发现,ef6 不能添加 到  .NET Standard2  的类库,因为不兼容, 6 以上的版本只能用于  .net 4.5 以上 只能用别的

  8. Centos6 Connect WiFi

    Centos6 Connect WiFi // 安装 wireless tools yum install wireless-tools dkms pciutils lsusb // 使用wlan命令 ...

  9. nginx Proxy Cache 配置

    总结一下 proxy cache 设置的常用指令及使用方法: proxy_cache proxy_cache zone | off 配置一块公用的内存区域的名称,该区域可以存放缓存的索引数据.注意:z ...

  10. Node.js 实战(一)之—防灾备措施

    前言 博客系统上线已经3个多月了,中间没有出现过宕机事故,一直稳定运行.自己写的代码还是挺严谨的,小小鼓励一下!上周对云服务器进行了一次内存扩容,扩容后重启了一次服务器.虽然过程很短,但是因重启后导致 ...