Selenium2(WebDriver)_如何判断WebElement元素对象是否存在

简介: 1. selenium中如果去寻找元素,而元素不存在的话,通常会抛出NoSuchElementException 导致测试失败,但有时候,我们需要去确保页面元素不存在,才是我们正确的验收条件下面的方法可以用来判定页面元素是否存在 1 public boolean doesWebElement...

1. selenium中如果去寻找元素,而元素不存在的话,通常会抛出NoSuchElementException 导致测试失败,但有时候,我们需要去确保页面元素不存在,才是我们正确的验收条件下面的方法可以用来判定页面元素是否存在

 1 public boolean doesWebElementExist(WebDriver driver, By selector)
 2 { 
 3 
 4         try 
 5         { 
 6                driver.findElement(selector); 
 7                return true; 
 8         } 
 9         catch (NoSuchElementException e) 
10         { 
11                 return false; 
12         } 
13 }         

 

2.一般有这样的应用场合,例如我们要验证在一个网站是否登录成功,那么可以通过判断登录之后是否显示相应元素: 

WebElement linkUsername = driver.findElement(By.xpath("//a[contains(text(),"+username+")]"));

return linkUsername.isDisplayed();
 
这一方法的前提是:该元素之前已经存在,仅仅需要判断是否被显示。
 
现在存在另一种场合,页面元素并不存在,即通过driver.findElement只能在超时之后得到NoSuchElementException的异常。
 
因此只好通过如下方法解决:
 1 boolean ElementExist (By Locator )
 2 {
 3   try
 4   {
 5     driver.findElement( Locator );
 6     return true;
 7   }
 8   catch(org.openqa.selenium.NoSuchElementException ex)
 9   {
10       return false;
11   }
12 }

 但这一方法仍然不理想,有这样两个问题: 

1、这一方法不属于任何一个page页,因此需要额外进行框架上的变更以支持这些功能函数,否则就必须在每一个用到该函数的page类写一遍。 
2、仍然需要等到超时才能得知结果,当需要频繁使用该函数的时候会造成相当的时间浪费。
 
3.
类似于seleniumRC中的isTextPresent 方法 
用xpath匹配所有元素(//*[contains(.,'keyword')]),判断是否存在包含期望关键字的元素。 
使用时可以根据需要调整参数和返回值。 
 1 public boolean isContentAppeared(WebDriver driver,String content) {  
 2       boolean status = false;  
 3       try {  
 4           driver.findElement(By.xpath("//*[contains(.,'" + content + "')]"));  
 5           System.out.println(content + " is appeard!");  
 6           status = true;  
 7       } catch (NoSuchElementException e) {  
 8           status = false;  
 9           System.out.println("'" + content + "' doesn't exist!"));  
10      }  
11      return status;  
12  }  

详细xpath介绍请见:http://www.w3school.com.cn/xpath/

   
4. Xpath 多重判断  
1 while(currentPageLinkNumber<MaxPage)
2 {
3   WebElement PageLink;
4   PageLink = driver.findElement(By.xpath("//a[@class = 'PageLink' and @title ='"+Integer.toString(currentPageLinkNumber+1)+"']"));
5   PageLink.click();
6   currentPageLinkNumber++;
7 //OtherOperation();
8 }

 

 
 
转自:http://blog.csdn.net/aerchi/article/details/8057544
 
目录
相关文章
|
8月前
|
Web App开发 JavaScript
2021最新Selenium真正绕过webdriver检测
2021最新Selenium真正绕过webdriver检测
192 0
|
8天前
|
Java 测试技术 定位技术
《手把手教你》系列技巧篇(二十三)-java+ selenium自动化测试-webdriver处理浏览器多窗口切换下卷(详细教程)
【4月更文挑战第15天】本文介绍了如何使用Selenium进行浏览器窗口切换以操作不同页面元素。首先,获取浏览器窗口句柄有两种方法:获取所有窗口句柄的集合和获取当前窗口句柄。然后,通过`switchTo().window()`方法切换到目标窗口句柄。在项目实战部分,给出了一个示例,展示了在百度首页、新闻页面和地图页面之间切换并输入文字的操作。最后,文章还探讨了在某些情况下可能出现的问题,并提供了一个简单的本地HTML页面示例来演示窗口切换的正确操作。
37 0
|
9月前
|
Web App开发 数据采集 JavaScript
Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
Youtube 是一个非常流行的视频分享平台,有时候我们可能想要爬取一些视频的信息,比如标题、播放量、点赞数等。但是有些信息并不是直接显示在网页上的,而是需要我们将鼠标悬停在某个元素上才能看到,比如视频的时长、上传时间等。这些信息被称为悬停文本,它们是通过 JavaScript 动态生成的,所以我们不能用普通的 HTML 解析方法来获取它们。那么,我们该如何用爬虫来获取 Youtube 的悬停文本呢?本文将介绍一种方法,使用 Selenium Chrome Webdriver 来模拟浏览器操作,获取 Youtube 的悬停文本。
118 0
Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
|
Web App开发 JavaScript 测试技术
Selenium Webdriver 简易教程2
Selenium Webdriver 简易教程2
92 0
|
Web App开发 前端开发 JavaScript
Selenium Webdriver 简易教程
Selenium Webdriver 简易教程
95 0
|
JavaScript
selenium webdriver执行远程 第三方js解决方案
selenium webdriver执行远程 第三方js解决方案
selenium webdriver执行远程 第三方js解决方案
|
API 索引
selenium源码通读·13 |webdriver/support分析
selenium源码通读·13 |webdriver/support分析
88 0
selenium源码通读·13 |webdriver/support分析
|
移动开发 JavaScript 前端开发
selenium源码通读·12 |webdriver/remote分析
selenium源码通读·12 |webdriver/remote分析
155 0
selenium源码通读·12 |webdriver/remote分析
|
存储 API
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析
68 0
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析
|
数据采集 Web App开发 Linux
selenium源码通读·10 |webdriver/common/proxy.py-Proxy类分析
selenium源码通读·10 |webdriver/common/proxy.py-Proxy类分析
86 0
selenium源码通读·10 |webdriver/common/proxy.py-Proxy类分析

热门文章

最新文章