Selenium中的隐式等待和显式等待
在Selenium中,“等待”在执行测试中起着重要作用。在本文中,您将学习Selenium中“隐式”和“显式”等待的各个方面。
在本文中,您将学习 -
1. 为什么我们需要在selenium中等待?
2. 隐瞒等待
3. 明确等待
4. 流利的等待
为什么我们需要在Selenium中等待?
大多数Web应用程序都是使用Ajax和Javascript开发的。当浏览器加载页面时,我们想要与之交互的元素可能以不同的时间间隔加载。
它不仅难以识别元素,而且如果元素未定位,它将抛出“ ElementNotVisibleException ”异常。使用Waits,我们可以解决此问题。
让我们考虑一个场景,我们必须在测试中使用隐式和显式等待。假设隐式等待时间设置为20秒,显式等待时间设置为10秒。
假设我们试图找到一个具有一些“ExpectedConditions ”(显式等待)的元素,如果该元素不在显式等待(10秒)定义的时间范围内,它将使用由隐式等待定义的时间帧(在抛出“ ElementNotVisibleException ” 之前20秒)。
Selenium Web驱动程序等待
- 隐含的等待
- 明确等待
隐含的等待
Selenium Web Driver借用了Watir隐式等待的想法。
隐式等待将告诉Web驱动程序在它抛出“No Such Element Exception”之前等待一定的时间。默认设置为0.一旦我们设置了时间,Web驱动程序将在抛出异常之前等待该时间。
在下面的示例中,我们声明了一个隐含的等待,时间范围为10秒。这意味着如果元素在该时间范围内不位于网页上,则会引发异常。
声明隐式等待:
语法:
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
package guru.test99;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class AppTest {protected WebDriver driver;@Testpublic void guru99tutorials() throws InterruptedException {System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;String eTitle = "Demo Guru99 Page";String aTitle = "" ;// 启动Chrome并将其重定向到基本网址driver.get("http://demo.guru99.com/test/guru99home/" );//最大化浏览器窗口driver.manage().window().maximize() ;//获取标题的实际值aTitle = driver.getTitle();//将实际标题与预期标题进行比较if (aTitle.equals(eTitle)){System.out.println( "Test Passed") ;}else {System.out.println( "Test Failed" );}//关闭浏览器driver.close();
}
}
代码说明
在上面的例子中,
考虑以下代码:
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
隐式等待将接受2个参数,第一个参数将接受时间作为整数值,第二个参数将接受时间测量,包括SECONDS,MINUTES,MILISECOND,MICROSECONDS,NANOSECONDS,DAYS,HOURS等。
明确等待
显式等待用于告诉Web驱动程序在抛出“ ElementNotVisibleException ”异常之前等待某些条件(预期条件)或超过最大时间。
显式等待是一种智能的等待,但它只能应用于指定的元素。显式等待提供比隐式等待更好的选项,因为它将等待动态加载的Ajax元素。
一旦我们声明显式等待,我们必须使用“ ExpectedCondtions ”,或者我们可以配置我们想要使用Fluent Wait检查条件的频率。这些天在实现我们使用Thread.Sleep()时通常不建议使用
在下面的示例中,我们创建引用等待“ WebDriverWait ”类并使用“ WebDriver ”引用进行实例化,并且我们给出的最大时间帧为20秒。
句法:
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
package guru.test99;import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;public class AppTest2 {protected WebDriver driver;@Testpublic void guru99tutorials() throws InterruptedException {System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );driver = new ChromeDriver(); WebDriverWait wait=new WebDriverWait(driver, 20);String eTitle = "Demo Guru99 Page";String aTitle = "" ;// 启动Chrome并将其重定向到Base URLdriver.get("http://demo.guru99.com/test/guru99home/" );//最大化浏览器窗口driver.manage().window().maximize() ;//获取标题的实际值aTitle = driver.getTitle();//将实际标题与预期标题进行比较if (aTitle.contentEquals(eTitle)){System.out.println( "Test Passed") ;}else {System.out.println( "Test Failed" );}WebElement guru99seleniumlink;guru99seleniumlink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")));guru99seleniumlink.click();}}
代码说明
考虑以下代码:
WebElement guru99seleniumlink;
guru99seleniumlink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")));guru99seleniumlink.click();
在上面的示例中,等待“ WebDriverWait ”类或“ ExpectedConditions ”中定义的时间量,以先发生者为准。
上面的Java代码声明我们正在等待网页上“ WebDriverWait ”类中定义的20秒时间帧的元素,直到满足“ ExpectedConditions ”并且条件为“ visibilityofElementLocated ”。
以下是可以在显式等待中使用的预期条件
· alertIsPresent()
· elementSelectionStateToBe()
· elementToBeClickable()
· elementToBeSelected()
· frameToBeAvaliableAndSwitchToIt()
· invisibilityOfTheElementLocated()
· invisibilityOfElementWithText()
· presenceOfAllElementsLocatedBy()
· presenceOfElementLocated()
· textToBePresentInElement()
· textToBePresentInElementLocated()
· textToBePresentInElementValue()
· titleIs()
· titleContains()
· visibilityOf()
· visibilityOfAllElements()
· visibilityOfAllElementsLocatedBy()
· visibilityOfElementLocated()
流利的等待
流畅的等待用于告诉Web驱动程序等待条件,以及在抛出“ElementNotVisibleException”异常之前我们想要检查条件的频率。
频率:设置具有时间范围的重复循环,以定期验证/检查条件
让我们考虑一个场景,其中元素以不同的时间间隔加载。如果我们声明显式等待20秒,该元素可能会在10秒,20秒甚至更长时间内加载。在抛出异常之前它会等到指定的时间。在这种情况下,流畅的等待是理想的等待使用,因为这将尝试以不同的频率找到元素,直到它找到它或最终的计时器用完为止。
句法:
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);
package guru.test99;import org.testng.annotations.Test;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;public class AppTest3 {protected WebDriver driver;@Testpublic void guru99tutorials() throws InterruptedException {System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );String eTitle = "Demo Guru99 Page";String aTitle = "" ;driver = new ChromeDriver();// 启动Chrome并将其重定向到Base URL driver.get("http://demo.guru99.com/test/guru99home/" );//最大化浏览器窗口driver.manage().window().maximize() ;//获取标题的实际值aTitle = driver.getTitle();//将实际标题与预期标题进行比较if (aTitle.contentEquals(eTitle)){System.out.println( "Test Passed") ;}else {System.out.println( "Test Failed" );}Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);WebElement clickseleniumlink = wait.until(new Function<Webdriver, WebElement>(){public WebElement apply(WebDriver driver ) {return driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"));}});//点击selenium链接clickseleniumlink.click();//关闭浏览器driver.close() ;}
}
代码说明
考虑以下代码:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);
在上面的例子中,我们通过忽略“ NoSuchElementException ” 来声明一个流畅的等待,超时为30秒,频率设置为5秒。
考虑以下代码:
public WebElement apply(WebDriver driver ) {return driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"));
}
我们创建了一个新功能来识别页面上的Web元素。(例如:这里的Web元素只不过是网页上的selenium链接)。
频率设置为5秒,最大时间设置为30秒。因此,这意味着它将每隔5秒检查网页上的元素,最长时间为30秒。如果元素位于此时间范围内,它将执行操作,否则将抛出“ ElementNotVisibleException ”
隐式等待与显式等待的区别
隐含的等待 |
明确等待 |
|
|
|
|
|
|
结论:
隐式,显式和流利等待是selenium中使用的不同等待。这些等待的使用完全基于以不同时间间隔加载的元素。在测试我们的应用程序或构建我们的框架时,始终不建议使用Thread.Sleep()。
如果你需要更多的selenium自动化的相关知识,欢迎继续关注我!当然你也可以加入我的群:175317069,一起进行学习交流,不仅仅只有自动化哦~
Selenium中的隐式等待和显式等待相关推荐
- Python网络爬虫Selenium页面等待:强制等待、隐式等待和显式等待
关于Python网络爬虫Selenium强制等待.隐式等待和显式等待的总结 from selenium import webdriver import time from selenium.webdr ...
- Selenium4+Python3系列 - Selenium的三种等待,强制等待、隐式等待、显式等待
为什么要设置元素等待 直白点说,怕报错,哈哈哈! 肯定有人会说,这也有点太直白了吧. 用一句通俗易懂的话就是:等待元素已被加载完全之后,再去定位该元素,就不会出现定位失败的报错了. 如何避免元素未加载 ...
- 自动化测试中,三种常用的等待方式,强制式(sleep) 、 隐式 ( implicitly_wait ) 、显式(expected_conditions)
在自动化测试总,主要有三种常用的等待方式,强制式(sleep) . 隐式 ( implicitly_wait ) .显式(expected_conditions). 一.强制式(sleep) ...
- 三种等待方式:强制等待、显式等待、隐式等待
我们在使用selenium的时候,会遇到一种定位不到的情况,因为web页面有一个加载的过程 当页面元素未出现时,去定位肯定是定位不到的,所以我们需要用到了'等待',该如何使用等待呢,让我们一起来探讨一 ...
- js 中的this,默认绑定、隐式绑定、显式绑定、new绑定、箭头函数绑定详解
壹 ❀ 引 工具猴-免费在线工具-在线工具箱- 可以说this与闭包.原型链一样,属于JavaScript开发中老生常谈的问题了,百度一搜,this相关的文章铺天盖地.可开发好几年,被几道this题安 ...
- Oracle隐式游标和显式游标
游标是什么?就是在内存开辟的一块临时存储空间. 1.Oracle隐式游标 1.1Oracle有常用的哪些隐式游标 1.2 Oracle隐式游标演示 -- 隐式游标 (使用的表为Oracle默认自带的e ...
- intent隐式和显式_Neo4j:使隐式关系成为显式和双向关系
intent隐式和显式 我最近阅读了Michal Bachman关于 Neo4j中双向关系的文章 ,他建议对于某些关系类型,我们对关系的方向不那么感兴趣,因此可以在查询时忽略它. 他使用以下示例显示N ...
- Neo4j:使隐式关系成为显式和双向关系
最近,我阅读了Michal Bachman关于 Neo4j中双向关系的文章 ,他建议对于某些关系类型,我们对关系的方向不那么感兴趣,因此可以在查询时忽略它. 他使用以下示例显示了Neo Technol ...
- oracle 游标内存自动释放,详解Oracle隐式游标和显式游标
游标是什么?就是在内存开辟的一块临时存储空间. 1.Oracle隐式游标 1.1Oracle有常用的哪些隐式游标 1.2 Oracle隐式游标演示 -- 隐式游标 (使用的表为Oracle默认自带的e ...
最新文章
- ubuntu16.04安装英伟达(NVIDIA)驱动——run文件安装
- Android View 事件分发机制详解
- ThreadLocal 和 InheritableThreadLocal
- 如何使用java synchronized进行线程同步 .
- WindowsPhone8可缩放图片控件的实现
- 从源码分析RocketMQ系列-Producer的invokeSync()方法
- java 程序包jar_把Java程序打包成jar文件包并执行
- Linux下逻辑卷LVM的管理和RAID磁盘阵列
- redis-集群(cluster)扫盲篇(一)
- 网上书店系统活动设计
- 如何使用MEGA软件构建系统发育树_速成实用经验
- C++多线程同步效率对比之临界区和原子锁
- 永远保持一颗奋斗的心
- KeyError: [] not found in axis_最IN拉花潮改“出圈”,欧拉白猫拓创无限可能_搜狐汽车...
- Cocos2dx-- 聊天系统之富文本(RichText)与字体对齐
- 全网首发游戏陪玩系统 语音聊天系统11月商业版源码 附教程
- C++ Vecor 清空内存
- 腾讯企业邮箱api java_腾讯企业邮箱API实现同步成员资料
- 重庆理工大学计算机考研资料汇总
- 为什么short_open_tag设置成On仍无效