一、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. Hibernate中@Embedded和@Embeddable注解

    在使用实体类生成对应的数据库表时,很多的时候都会遇到这种情况:在一个实体类中引用另外的实体类,一般遇上这种情况,我们使用@OneToOne.@OneToMany.@ManyToOne.@ManyToM ...

  2. hdu2175之找规律

    汉诺塔IX Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. WinForm特效:同时让两个窗体有激活效果

    windows api,一个窗体激活的时候给另外一个发消息 using System; using System.Windows.Forms; using System.Runtime.Interop ...

  4. jQuery调用ajax获取json格式数据

    <body> <div>点击按钮获取音乐列表</div> <input type="button" id="button&quo ...

  5. Node.js:创建应用+回调函数(阻塞/非阻塞)+事件循环

    一.创建应用 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi.从这个角度看,整个"接收 HTTP ...

  6. Android WebView File域同源策略绕过漏洞浅析

       0x00     我们首先讲一个webView这种方法的作用: webView.getSettings().setAllowFileAccessFromFileURLs(false);     ...

  7. windows下读取Linux分区软件

    导读 ext3等日志型文件系统是Linux中被广泛应用的,通常是许多流行Linux发行版默认的文件系统.etx4也是Linux下的日志型文件系统,被设计作为ext3的继任者.他消除了64位存储限制,是 ...

  8. 【转】js-ES6学习笔记-Symbol

    原文:https://www.cnblogs.com/zczhangcui/p/6435652.html https://blog.mgechev.com/2017/09/16/developing- ...

  9. 转:mac下安装Sublime Text

    转:http://blog.sina.com.cn/s/blog_559d66460101cab0.html 正版的买个license其实并不贵,定价为70美元.如果不买license,也可acces ...

  10. GNU General Public License v3.0

    Version 3, 29 June 2007       Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/& ...