【Selenium】9显示等待、隐式等待
来源:互联网 发布:淘宝818 编辑:程序博客网 时间:2024/06/04 20:12
显示等待
WebDriverWait
elementToBeCllickable(By locator)页面元素是否出现可用(enabled)和可被点击
elementToBeSelected(WebElement element)页面元素处于被选中状态
presenceOfElementLocated(By locator)页面元素在页面中存在
textToBePresentInElement(By locator)在页面元素中是否包含特定的文本
textToBePresentInElementValue(By locator,java.lang.Stringtext)页面元素值
titleContains(java.lang.String.title)标题
WebDriverWait wait=new WebDriverWait(driver,10);//获取内容出现wait.until(ExpectedConditions.titleContains("baoyu"));System.out.println("baoyu出现了");//列表选项是否选中WebElement element = driver.findElement(By.id(""));wait.until(ExpectedConditions.elementToBeSelected(element));//列表选项是否可被单击wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));//是否在页面中wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("")));//是否包含文字WebElement p= driver.findElement(By.id(""));wait.until(ExpectedConditions.textToBePresentInElement(p,"爱吃水果"));
超时抛出TimeOutException,默认500毫秒
public class WaitToReturnElement { /* * 设置超时时间为5秒,返回指定xpath的WebElement * */ public static WebElement waitForByXpath(final WebDriver driver,final String xpath) { WebDriverWait wait = new WebDriverWait(driver, 5); return wait.until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver arg0) { return driver.findElement(By.xpath(xpath)); } }); } /* * 设置超时时间为10秒,返回指定id的WebElement * */ public static WebElement waitForById(final WebDriver driver,final String id) { WebDriverWait wait = new WebDriverWait(driver, 10); return wait.until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver arg0) { return driver.findElement(By.id(id)); } }); } /* * 设置超时时间为10秒,返回指定xpath的WebElement是否出现 * */ public static Boolean isElementDisplayed(final WebDriver driver,final String xpath) { WebDriverWait wait = new WebDriverWait(driver, 10); return wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver arg0) { return driver.findElement(By.xpath(xpath)).isDisplayed(); } }); } }
//判断Ajax请求是否加载完成public static Boolean ajaxRequestFinish(final WebDriver driver) { WebDriverWait wait = new WebDriverWait(driver, 10); return wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver arg0) { JavascriptExecutor js = (JavascriptExecutor) driver; return (Boolean) js.executeScript("return jQuery.active==0"); } });}
//判断页面元素是否存在
WebDriver driver; private Boolean IsElementPresent(By by){ try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } @Testpublic void testsElementPresent(){ driver.get(""); if(IsElementPresent(By.id(""))){ WebElement element = driver.findElement(By.id("")); if (element.isEnabled()==true){ }else { Assert.fail("未找到"); } } }
ExpectedCondition
等待元素直到可点击状态WebDriverWait wait=new WebDriverWait(driver,10);wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
隐式等待
查找WebDriver无法使用的元素时等待,默认0,生命周期整个WebDriver,跑出NoSuchElementException异常
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assert.fail("没有找到元素”);
阅读全文
0 0
- 【Selenium】9显示等待、隐式等待
- Selenium--显示等待和隐式等待
- Selenium之等待函数--强制等待,显示等待,隐式等待
- Selenium显示等待和隐式等待的区别
- Selenium控制流程(显示等待与隐式等待)
- selenium 的显示等待与隐式等待
- selenium 显示等待,隐士等待
- [selenium] selenium+java+testNG 显示等待
- [selenium] selenium+java+TestNG 自定义显示等待
- selenium+Java等待模式(显式等待+隐式等待)
- selenium的webdriver三种等待方式(显式等待WebDriverWait+implicitly_wait隐式等待+sleep强制等待)
- Selenium学习9--显示等待,判断页面元素是否存在
- selenium显示等待与隐士等待的区别
- Selenium2设置隐式等待和显示等待
- (转)implicitly_wait()隐式等待和explicit_wait()显示等待
- selenium--全局等待,智能等待
- selenium 常用的显式等待
- Selenium(Webdriver)显式等待封装
- 创建 overlay 网络
- linux gcc 编译时头文件和库文件搜索路径
- springmvc常用注解标签详解
- 基于ajax的三层,实现数据库增删改查基础(一DAL)
- Retrofit2.0联网框架详解
- 【Selenium】9显示等待、隐式等待
- Python3.5 报错:NameError: name 'reload' is not defined
- PHP文件操作函数
- 机器学习(Machine Learning)&深入学习(Deep Learning)资料
- Redis高速内存缓冲平台可视化监控之RedisLive
- POI项目
- UIView如何设置部分圆角
- linux查看用户操作或程序执行的一些信息
- Request获取用户真实IP