Selenium学习9--显示等待,判断页面元素是否存在
2016-09-15 16:58
573 查看
html代码如下:
<html lang="en"> <head> <title>your favorite fruits</title> </head> <body> <p>select ur perfer fruit</p> <br> <select name='fruit'> <option id='peach' value='taozi'>peach</option> <option id='watermelon' value='xigua'>watermelon</option> </select> <br> <input type='checkbox'>Do you like fruits?</input> <br><br> <input type=text" id="text" value="Watermelons are so sweet this year!">This is Input box</input> </body> </html>
@Test public void testExplicitWait(){ driver.get("E:\\Java\\selenium_data\\html\\obviouswait.html"); //声明一个WebDriverWait对象,设定复发条件的最长时间待定10s WebDriverWait wait = new WebDriverWait(driver,10); //调用ExpectedConditions的titleContains方法判断页面title属性是否包含"fruits"字段 wait.until(ExpectedConditions.titleContains("fruits")); WebElement select = driver.findElement(By.xpath("//option[@id='peach']")); //elementToBeSelected(Element element)判断"peach"是否被选中 wait.until(ExpectedConditions.elementToBeSelected(select)); //elementToBeClickable(String )判断复选框对象是否可以被点击 wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p"))); //presenceOfElementLocated 判断元素是否存在 wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//x"))); //textToBePresentInElement判断字样是否在元素中显示 WebElement p = driver.findElement(By.xpath("//p")); wait.until(ExpectedConditions.textToBePresentInElement(p, "select ur perfer fruit")); }
自定义显示等待
@Test public void testCustomExplicitWait(){ driver.get("E:\\Java\\selenium_data\\html\\obviouswait.html"); try{ //显示等待判断是否可以从页面获取文字输入框对象, 如果可以获取, 则执行后续测试逻辑 WebElement textInputBox = (new WebDriverWait(driver,10).until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver driver) { return driver.findElement(By.xpath("//*[@type='text']")); } })); Assert.assertEquals("Watermelons are so sweet this year!", textInputBox.getAttribute("value")); Boolean containTextFlag = (new WebDriverWait(driver,10).until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver driver) { return driver.findElement(By.xpath("//p")).getText().equals("perfer fruit"); } })); Assert.assertTrue(containTextFlag); }catch(Exception e){ e.printStackTrace(); } }
判断页面元素是否存在
private boolean isElementPresent(By by){ try{ driver.findElement(by); return true; }catch(Exception e){ return false; } } @Test public void testIsElementPresent(){ driver.get("http://www.sogou.com"); if(isElementPresent(By.id("query"))){ WebElement searchInputBox = driver.findElement(By.id("query")); if(searchInputBox.isEnabled()){ searchInputBox.sendKeys("元始金章"); } }else{ Assert.fail("元素没有找到,zz"); } }
相关文章推荐
- selenium 判断页面元素是否存在
- Selenium2学习-029-WebUI自动化实战实例-027-判断元素是否存在
- jQuery 判断页面元素是否存在的代码
- javascript(jquery)中判断一个元素在页面中是否存在的方法(转载-来自CSDN)
- jQuery判断页面元素(对象)是否存在
- jQuery 判断页面元素是否存在的代码
- [Javascript]jquery判断页面元素是否存在
- jQuery判断元素是否显示和存在
- javascript(jquery)中如何判断一个元素在页面中是否存在
- selenium webdriver学习-怎么等待页面元素加载完成
- jquery判断页面元素是否存在
- js 判断页面元素是否存在
- jQuery 判断页面元素是否存在的代码
- js 判断一个元素是否在页面中存在
- jQuery 判断页面元素是否存在的代码
- selenium+pyton关于判断元素是否存在的问题
- 算法学习笔记----判断集合S中是否存在有两个其和等于x的元素
- 用jQuery判断页面元素是否存在的方法
- jquery判断页面上某个ID元素是否存在的方法