分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

Implicit Wait

Selenium WebDriver has borrowed the idea of implicit waits from Watir. This means that we can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page. We should note that implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

Fluent Wait

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions while waiting, such as NoSuchElementExceptions when searching for an element on the page.

// Waiting 30 seconds for an element to be present on the page, checking for its presence once every 5 seconds.
Wait wait = new FluentWait(driver).withTimeout(30, SECONDS).pollingEvery(5, SECONDS).ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {public WebElement apply(WebDriver driver) {return driver.findElement(By.id("foo"));}
});

Explicit Wait

It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt ExpectedConditions to wait for elements to become clickable, visible, invisible, etc.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

Differences Between Implicit, Explicit & Fluent Wait

Implicit Wait: During Implicit Wait if the Web Driver cannot find it immediately because of its availability, it will keep polling (around 250 milli seconds) the DOM to get the element. If the element is not available within the specified time an NoSuchElementException will be raised. The default setting is zero. Once we set a time, the Web Driver waits for the period of time.

Explicit Wait: There can be instance when a particular element takes more than a minute to load. In that case you definitely not like to set a huge time to Implicit Wait, as if you do this your browser will going to wait for the same time for every element.

To avoid that situation you can simply put a separate time on the required element only. By following this your browser implicit wait time would be short for every element and it would be large for specific element.

Fluent Wait: Let’s say you have an element which sometime appears in just 1 second and some time it takes minutes to appear. In that case it is better to use fluent wait, as this will try to find element again and again until it finds it or until the final timer runs out.

Solutions: There are several scenarios when we try to find an element in the webpage DOM:

  • An element not being present at all in the DOM
  • An element being present in the DOM but not visible
  • An element being present in the DOM but not enabled (i.e. clickable)

There are pages which get displayed with the JavaScript, the elements are already present in the webpage DOM, but are not visible. The implicit wait only waits for an element to appear in the DOM, so it returns immediately, but when you try to interact with the element you get a NoSuchElementException. You could test this hypothesis by writing a helper method that explicit wait for an element to be visible (or clickable similarly).

public WebElement getWhenVisible(By locator, int timeout) {
WebElement element = null;
WebDriverWait wait = new WebDriverWait(driver, timeout);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
return element;
}
public void clickWhenReady(By locator, int timeout) {
WebElement element = null;
WebDriverWait wait = new WebDriverWait(driver, timeout);
element = wait.until(ExpectedConditions.elementToBeClickable(locator));
element.click();
}

Selenium - Differences Between Implicit, Explicit Fluent Wait相关推荐

  1. C#(4) implicit explicit

    最近几节课学的知识点的量有很大增长,加上上机和托福考试的临近,这里练习的所有代码就不走形式全贴出来了,找我自己觉得又不熟悉的点贴出来,争取简明扼要,恩恩 这次主要看接口的问题: 接口可以多继承,有些时 ...

  2. C++explicit与implicit

    C++explicit与implicit explicit与implicit是C++关键字,其用处是修饰类构造函数. explicit只能用于修饰只有一个参数的类构造函数,它的作用是表示此类构造函数是 ...

  3. mpf4_定价欧式美式障碍Options_CRR_Leisen-Reimer_Greeks_二叉树三叉树网格_Finite differences(显式隐式)Crank-Nicolson_Imp波动率

    A derivative[dɪˈrɪvətɪv](金融)衍生工具(产品)  is a contract whose payoff depends on the value of some underl ...

  4. Delphi DLL制作和加载 Static, Dynamic, Delayed 以及 Shared-Memory Manager

    一 Dll的制作一般分为以下几步: 1 在一个DLL工程里写一个过程或函数 2 写一个Exports关键字,在其下写过程的名称.不用写参数和调用后缀. 二 参数传递 1 参数类型最好与window C ...

  5. 考研英语 | 单词之间

    目录 Day 1 -able- 能够 -ab- 否定.不 -academy- 学院 -act-.-ag- 行动,作用:表演,表现:法案,法令 -acu-.-acr- 尖锐,锐利 -add- 加,增加 ...

  6. HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档

    Hibernate参考文档 3.1.2 目录 前言 1. 翻译说明 2. 版权声明 1.Hibernate入门 1.1.前言 1.2.第一部分-第一个Hibernate应用程序 1.2.1.第一个cl ...

  7. Hibernate中文参考文档(JFIS)

    HIBERNATE - 符合Java习惯的关系数据库持久化      下一页 HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.0.4 目录 前言 1. 翻译 ...

  8. 求教大牛!关于后缀树

    发信人: keerling (keer), 信区: Algorithm 标  题: 求教大牛!关于后缀树 发信站: 水木社区 (Wed Nov  7 16:52:12 2007), 站内 不知道这帖子 ...

  9. python怎么导入包-Python模块导入与包构建最佳实践

    [TOC] 最开始写程序的时候,都是一个文件里输入几行源码(python 的一个 web 框架bottle就特别强调自己是单文件框架).随着程程式变大变复杂,一个文件很难承载如此多的功能,因此将代码拆 ...

最新文章

  1. Linux 系统上的库文件生成与使用
  2. Visual Studio 2019 16.1 第二个预览版发布
  3. 2021-03-28 收敛性常用一阶微分方程
  4. python分析nginx日志
  5. CloudCC CRM探讨如何建立完善的服务体系
  6. Qt Creator 第一个插件
  7. 【codeforces 534B】Covered Path
  8. c/c++编译的程序占用的内存分配
  9. 一款云迁移产品的成长史
  10. 使用win7超级终端连接华为交换机并配置端口镜像
  11. 关于动态库so的makefile编写
  12. Android ViewPage使用
  13. 计算机网络协议指的是tcp ip协议吗,计算机除了有网络协议也就是TCP/IP协议以外,还有什么协议呢?...
  14. 用python进行数据分析举例说明_《利用python进行数据分析》读书笔记 --第一、二章 准备与例子...
  15. App首屏接口性能优化
  16. 上海往事之2015-07股市风云录
  17. TensorFlow 高维tenso常用工具函数
  18. “红领巾”牵手“蓝朋友”-记杭州澎博小学新201中队2019年暑期活动
  19. WORD,PDF中最护眼的颜色
  20. java做一个打地鼠小游戏

热门文章

  1. 基于墨刀的web界面设计
  2. Fancy PCA图像扩充总结(附代码)
  3. Python的赋值操作
  4. 启动谷歌浏览器chrome,提示”没有注册类”
  5. SAP 原因代码的两种配置优缺点
  6. PyQt5实现图片缩放、旋转
  7. 中央处理器(CPU)的组成
  8. 通过源码发现nltk.Text.similar相似度衡量标准
  9. oneapm php实例,号外!OneAPM Ai PHP 探针出 Windows 版本啦!
  10. 详细说明UML类图是什么?