Selenium控制流程(显示等待与隐式等待)
来源:互联网 发布:淘宝店铺收藏 编辑:程序博客网 时间:2024/06/05 15:08
一:WEBELEMEN检查元素状态
1:isEnable()检查元素是否被启用
2:isSelected()检查元素是否被选中
3:isDisplay()检查元素是否可见
二:SELENIUM隐式等待与显示等待
1:隐式等待
driver.manage().timeouts().implicitlyWait(“等待时间”,“等待时 间单位,如:毫秒‘秒’”)
该隐式等待作用于全局,一般不提倡使用
2:显示等待
webDriverWait类
实例方法<1>:webDriverWait wait=new webDriverWait(参数一为驱动,参数二为等待时间)
实例方<2>: webDriverWait wait=new webDriverWait()
wait.until(ExpectedConditions.presenceOfElementLocated
(By.id(“id ”)));
示例:
package cn.wait;import java.util.concurrent.TimeUnit;import org.junit.Ignore;import org.junit.Test;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.FluentWait;import org.openqa.selenium.support.ui.WebDriverWait;import com.google.common.base.Function; /** * 设置火狐路径时,必须在创建驱动之前 * @author zx * */public class WaitDemo { /** * 隐式等待,针对全局,默认时间为3000 */ @Ignore//忽略 public void implicitWait(){ //设置火狐路径 System.setProperty("webdriver.firefox.bin", "C:\\software\\Tool\\firefox\\firefox.exe"); WebDriver driver=new FirefoxDriver(); String url; url="http://www.baidu.com"; //设置访问路径、 driver.get(url+"/"); //隐式等待针对全局 driver.manage().timeouts().implicitlyWait(5,TimeUnit.MILLISECONDS); driver.findElement(By.id("kw")).sendKeys("selenium学习"); driver.findElement(By.id("su")).click(); } /** * 显示等待===webdriverWait 方法一 * 针对将要执行的代码处, */ @Ignore public void showWait(){ //设置火狐路径 System.setProperty("webdriver.firefox.bin", "C:\\software\\Tool\\firefox\\firefox.exe"); WebDriver driver=new FirefoxDriver(); String url; url="http://www.baidu.com"; //设置访问路径、 driver.get(url+"/"); //设置显示等待,创建主类、 WebDriverWait wait=new WebDriverWait(driver, 3000); //判断页面上该元素是否存在 WebElement webElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("su"))); boolean displayed = webElement.isDisplayed(); //判断该元素是否可见 System.out.println(displayed+"============="); driver.findElement(By.id("kw")).sendKeys("hello"); } /** * 显示等待 ==通过匿名内部类 ===方法二 */ @Ignore public void newShowWait(){ //设置火狐路径 System.setProperty("webdriver.firefox.bin", "C:\\software\\Tool\\firefox\\firefox.exe"); WebDriver driver=new FirefoxDriver(); String url; url="http://www.baidu.com"; //设置访问路径、 driver.get(url+"/"); //设置显示等待,创建主类、 WebDriverWait wait=new WebDriverWait(driver, 3000); try { //创建匿名内部类 找到元素返回,未找到元素抛出异常 需要抛异常 WebElement webElementSU = wait.until(new ExpectedCondition<WebElement>() { //重写方法 public WebElement apply(WebDriver driver) { return driver.findElement(By.id("su")); } }); //判断该元素是否可见// webElementSU.isDisplayed(); System.out.println("su元素的状态为"+webElementSU.isDisplayed()); driver.findElement(By.id("kw")).sendKeys("hello"); driver.quit();//退出浏览器 } catch (Exception e) { System.out.println("未找到元素"); } } //=============================================================== /** * ===============显示等待封装============================== */ public boolean wait(WebDriver driver,long timeout){ boolean result=true; try { WebDriverWait wait=new WebDriverWait(driver,timeout); wait.until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver driver) { WebElement findElement = driver.findElement(By.id("su")); return findElement; } }); } catch (Exception e) { result=false; } return result; } /** * 调用封装实现显式等待 */ @Ignore public void interior(){ //设置火狐路径 System.setProperty("webdriver.firefox.bin", "C:\\software\\Tool\\firefox\\firefox.exe"); WebDriver driver=new FirefoxDriver(); String url; url="http://www.baidu.com"; //设置访问路径、 driver.get(url+"/"); //调用封装 boolean findEle = this.wait(driver, 3000); if(findEle==true){ driver.findElement(By.id("kw")).sendKeys("hello"); System.out.println("代码执行结束"); }else{ System.out.println("su元素未找到"); } } /** * 自定义实现显示等待 */ @Test public void nameWait() { //设置火狐路径 System.setProperty("webdriver.firefox.bin", "C:\\software\\Tool\\firefox\\firefox.exe"); WebDriver driver=new FirefoxDriver(); String url; url="http://www.baidu.com"; //设置访问路径、 driver.get(url+"/"); //WebDriverWait实现了fluenWait类 所以我们可以创建该fluentwait类 ,重写里面的unit方法 FluentWait<WebDriver> fwait=new FluentWait<WebDriver>(driver); //设置多少时间查询一次 fwait.pollingEvery(10, TimeUnit.MICROSECONDS); //设置超时时间 fwait.withTimeout(5, TimeUnit.SECONDS); //重写 WebElement webElement = fwait.until(new Function<WebDriver,WebElement>(){ public WebElement apply(WebDriver driver) { return driver.findElement(By.id("kw")); } }); System.out.println(webElement.isDisplayed()); webElement.sendKeys("一剪梅"); driver.findElement(By.id("su")).click(); }}
0 0
- Selenium控制流程(显示等待与隐式等待)
- selenium 的显示等待与隐式等待
- 【Selenium】9显示等待、隐式等待
- Selenium--显示等待和隐式等待
- Selenium之等待函数--强制等待,显示等待,隐式等待
- Selenium显示等待和隐式等待的区别
- selenium 显示等待,隐士等待
- selenium显示等待与隐士等待的区别
- [selenium] selenium+java+testNG 显示等待
- [selenium] selenium+java+TestNG 自定义显示等待
- python selenium 显示等待WebDriverWait与条件判断expected_conditions举例
- selenium+Java等待模式(显式等待+隐式等待)
- selenium的webdriver三种等待方式(显式等待WebDriverWait+implicitly_wait隐式等待+sleep强制等待)
- selenium webdriver 学习总结-Selenium 控制测试流_补充显示等待(五)
- selenium webdriver 学习总结-Selenium 控制测试流(五)-补充显示等待
- selenium webdriver 学习总结-Selenium 控制测试流_补充显示等待(五)
- Selenium2设置隐式等待和显示等待
- (转)implicitly_wait()隐式等待和explicit_wait()显示等待
- 企业用车管理系统流程图
- eclipse导入IntelliJ IDEA的maven项目
- cocos2d-x 2.2.2版本环境搭建(转)
- 制作一个漂亮的 vim 状态栏
- CSDN-markdown编辑器语法——字体、字号与颜色
- Selenium控制流程(显示等待与隐式等待)
- 递归层次汇总查询
- flume配置-生产环境下从文件目录下将日志上传到s3
- 复选框练习
- 关于消息循环
- 鼠标右键事件
- 44-中断系统调用与自动重启动
- tcp中RACK算法
- 特征缩放(Feature Scaling)