一、sleep () 休眠方法   --time 固定等待

在开发自动化框架过程中,最忌讳使用Python自带模块的time的sleep方法进行等待,虽然可以自定义等待时间,但当网络条件良好时,

依旧按照预设定的时间继续等待,导致整个项目的自动化时间无限延长。不建议使用。

二、implicitly_wait(隐式等待)

隐式等待是通过一定的时长等待页面上某元素加载完成。如果超出了设置的时长元素还没有被加载,则抛出NoSuchElementException 异常。

implicitly_wait 方法来实现隐式等,默认设置为0

隐式等待会作用于整个driver 周期,所以只需要写到get 后面即可

三、WebDriverWait(显示等待)

显示等待使WebDriver 等待某个条件成立时继续执行,否则在达到最长时长时抛出超时异常(TimeoutException)

源代码参考:

import time
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException POLL_FREQUENCY = 0.5 # How long to sleep inbetween calls to the method
IGNORED_EXCEPTIONS = (NoSuchElementException,) # exceptions ignored during calls to the method class WebDriverWait(object):
def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
"""Constructor, takes a WebDriver instance and timeout in seconds. :Args:
- driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
- timeout - Number of seconds before timing out
- poll_frequency - sleep interval between calls
By default, it is 0.5 second.
- ignored_exceptions - iterable structure of exception classes ignored during calls.
By default, it contains NoSuchElementException only. Example:
from selenium.webdriver.support.ui import WebDriverWait \n
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
until_not(lambda x: x.find_element_by_id("someId").is_displayed())
"""
self._driver = driver
self._timeout = timeout
self._poll = poll_frequency driver :浏览器驱动
timeout :最长超时时间,默认以秒为单位
poll_frequency :检测的间隔(步长)时间,默认为0.5S
ignored_exceptions :超时后的异常信息,默认情况下抛出 NoSuchElementException异常
WebDriverWait()  一般有until() 或 until_not() 方法配合使用
引入方法:
from selenium.webdriver.support.wait import  WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
driver.find_element_by_css_selector('xx')
# WebDriverWait(driver,10).until(Ec.title_is('xx')) #等什么出现则继续执行
# WebDriverWait(driver,10).until_not() #等什么消失则继续执行,不常用
# e = WebDriverWait(driver,10).until(Ec.presence_of_element_located(('id','i1')))#等什么出现则继续执行
expected_conditions 类提供的预期条件判断方法如下:
1 title_is: 判断当前页面的title是否完全等于(==)预期字符串,返回布尔值
2 title_contains : 判断当前页面的title是否包含预期字符串,返回布尔值
3 presence_of_element_located : 判断某个元素是否被加到了dom树里,并不代表该元素一定可见
4 visibility_of_element_located : 判断某个元素是否可见. 可见代表元素非隐藏,并且元素的宽和高都不等于0
5 visibility_of : 跟上面的方法做一样的事情,只是上面的方法要传入locator,这个方法直接传定位到的element就好了
6 presence_of_all_elements_located : 判断是否至少有1个元素存在于dom树中。举个例子,如果页面上有n个元素的class都是‘column-md-3‘,
那么只要有1个元素存在,这个方法就返回True
7 text_to_be_present_in_element : 判断某个元素中的text是否 包含 了预期的字符串
8 text_to_be_present_in_element_value : 判断某个元素中的value属性是否 包含 了预期的字符串
9 frame_to_be_available_and_switch_to_it : 判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False
10 invisibility_of_element_located : 判断某个元素中是否不存在于dom树或不可见
11 element_to_be_clickable : 判断某个元素中是否可见并且是enable的,这样的话才叫clickable
12 staleness_of : 等某个元素从dom树中移除,注意,这个方法也是返回True或False
13 element_to_be_selected : 判断某个元素是否被选中了,一般用在下拉列表
14 element_selection_state_to_be : 判断某个元素的选中状态是否符合预期
15 element_located_selection_state_to_be : 跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locator
16 alert_is_present : 判断页面上是否存在alert

Selenium - 设置元素等待的更多相关文章

  1. Python+Selenium设置元素等待

    显式等待 显式等待使 WebdDriver 等待某个条件成立时继续执行,否则在达到最大时长时抛弃超时异常 (TimeoutException). #coding=utf-8 from selenium ...

  2. selenium webdriver——设置元素等待

    如今大多数Web应用程序使用ajax技术,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加了困难, 如果因为在加载某个元素时延迟而造成ElementNotVisibl ...

  3. Selenium(十一):设置元素等待、上传文件、下载文件

    1. 设置元素等待 前面我们接触了几个元素等待方法,sleep.implicitly_wait方法,这一章我们就来整体学一下. 现在大多数Web应用程序使用的都是AJAX技术.当浏览器加载页面时,页面 ...

  4. Selenium 2自动化测试实战13(设置元素等待)

    一.设置元素等待 若在加载某个元素时延迟而造成的ElementNotVisbleException的情况出现,那么就会降低自动化脚本的稳定性,可以通过设置元素等待改善这种问题造成的不稳定. webdr ...

  5. 【亲测显式等待】Selenium:元素等待的4种方法

    Selenium:元素等待的4种方法 1.使用Thread.sleep(),这是最笨的方法,但有时候也能用到而且很实用.   2.隐式等待,隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉We ...

  6. Python +selenium之设置元素等待

    注:本文转载http://www.cnblogs.com/mengyu/p/6972968.html 当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给元素的定位增加了困难.如果因为在加 ...

  7. selenium - webdriver - 设置元素等待

    隐式等待:implicitly_wait(value), value默认是0 from selenium import webdriverfrom selenium.common.exceptions ...

  8. python + slenium自动化测试设置元素等待

    WebDriver 提供了两种类型的等待:显式等待和隐式等待. 显式等待 显式等待使 WebdDriver 等待某个条件成立时继续执行,否则在达到最大时长时抛出超时异常 (TimeoutExcepti ...

  9. web自动化之selenium(四)元素等待

    隐式等待 说明 隐式等待是通过设置一定时长的等待,让页面上的某些元素能过加载出来,如果超过了设置的时间还没有加载出来则抛出(NoSuchelementException异常),默认单位为"秒 ...

随机推荐

  1. 浏览器User-Agent的详细信息

    PC端: safari 5.1 – MACUser-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit ...

  2. 电子书mobi的格式详解

    https://wiki.mobileread.com/wiki/MOBI#Format Like PalmDOC, the Mobipocket file format is that of a s ...

  3. Android NDK开发----- JNI多线程

    一.概述 JNI编程和Linux上的C/C++编程还是挺相似的,每次java调用JNI中的函数时都会传入有关JVM的一些参数(如JNIEnv,jobject),每次JNI回调java中的方法时都要通过 ...

  4. vs 字体

    看代码看得眼疼不能不说是程序员的恶梦,那么,选择适当的字体也算是对自己的救赎吧.周末闲得无聊,在网上乱逛,搜索了一些资料整理一下给大家分享,仅作记录而已,参考使用: 1.一个编程人员痛苦的选择 一般适 ...

  5. c语言打印uint64, int64

    http://stackoverflow.com/questions/9225567/how-to-print-a-int64-t-type-in-c

  6. Jmeter--google plugin插件监控被測系统资源方法

    一.插件准备 1.插件下载地址 http://jmeter-plugins.org/downloads/all/ 下面有两个版本号的.1.1.2和1.1.3.注意Jmeter版本号 1.1.2支持Jm ...

  7. python 网络请求类库 requests 使用

    python 网络请求类库 requests 使用 requests是 为python封装的强大 REST 操作类库 githubhttps://github.com/kennethreitz/req ...

  8. vue - router 起步

    官方API:https://router.vuejs.org/zh/guide/#javascript vue-cli for index.js export default new Router({ ...

  9. 右键菜单 GenericMenu

    http://www.cnblogs.com/zhaoqingqing/p/3799294.html 自定义窗口中使用右键菜单: // This example shows how to create ...

  10. 从业者生存质量报告之,教育行业里的IT男

    当教育遇上互联网,非常多传统教育机构都卡在了技术这道门槛上. 一位教育机构创始人曾这样感叹说:"技术须要文化基因.氛围.教育行业不知道技术这帮兄弟须要什么样的文化,什么样的工作氛围,怎么管理 ...