非常简单的源码,敬请各位小主参阅。若有不足之处,敬请大神指正,不胜感激!

  1. /**
  2. * Verify the element exist or not
  3. *
  4. * @author Aaron.ffp
  5. * @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $
  6. *
  7. * @param by : By
  8. *
  9. * @return boolean
  10. */
  11. public boolean isElementExist(By by){
  12. boolean exist = false;
  13.  
  14. try {
  15. this.webdriver.findElement(by);
  16. exist = true;
  17. } catch (NoSuchElementException e) {
  18. this.logger.error(e);
  19. }
  20.  
  21. return exist;
  22. }
  23.  
  24. /**
  25. * Verify the element exist or not(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
  26. *
  27. * @author Aaron.ffp
  28. * @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $
  29. *
  30. * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
  31. *
  32. * @return boolean
  33. */
  34. public boolean isElementExist(String locator){
  35. boolean exist = false;
  36.  
  37. /* ID */
  38. try {
  39. this.webdriver.findElement(By.id(locator));
  40. exist = true;
  41. return exist;
  42. } catch (NoSuchElementException e) {
  43. this.logger.error(e);
  44. }
  45.  
  46. /* name */
  47. try {
  48. this.webdriver.findElement(By.name(locator));
  49. exist = true;
  50. return exist;
  51. } catch (NoSuchElementException e) {
  52. this.logger.error(e);
  53. }
  54.  
  55. /* cssSelector */
  56. try {
  57. this.webdriver.findElement(By.cssSelector(locator));
  58. exist = true;
  59. return exist;
  60. } catch (NoSuchElementException e) {
  61. this.logger.error(e);
  62. }
  63.  
  64. /* Xpath */
  65. try {
  66. this.webdriver.findElement(By.xpath(locator));
  67. exist = true;
  68. return exist;
  69. } catch (NoSuchElementException e) {
  70. this.logger.error(e);
  71. }
  72.  
  73. /* linkText */
  74. try {
  75. this.webdriver.findElement(By.linkText(locator));
  76. exist = true;
  77. return exist;
  78. } catch (NoSuchElementException e) {
  79. this.logger.error(e);
  80. }
  81.  
  82. /* className */
  83. try {
  84. this.webdriver.findElement(By.className(locator));
  85. exist = true;
  86. return exist;
  87. } catch (NoSuchElementException e) {
  88. this.logger.error(e);
  89. }
  90.  
  91. /* partialLinkText */
  92. try {
  93. this.webdriver.findElement(By.partialLinkText(locator));
  94. exist = true;
  95. return exist;
  96. } catch (NoSuchElementException e) {
  97. this.logger.error(e);
  98. }
  99.  
  100. /* tagName */
  101. try {
  102. this.webdriver.findElement(By.tagName(locator));
  103. exist = true;
  104. return exist;
  105. } catch (NoSuchElementException e) {
  106. this.logger.error(e);
  107. }
  108.  
  109. return exist;
  110. }
  111.  
  112. /**
  113. * Verify the element exist or not(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
  114. *
  115. * @author Aaron.ffp
  116. * @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $
  117. *
  118. * @param webdriver : WebDriver
  119. * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
  120. *
  121. * @return boolean
  122. */
  123. public boolean isElementExist(WebDriver webdriver, String locator){
  124. boolean exist = false;
  125.  
  126. /* ID */
  127. try {
  128. webdriver.findElement(By.id(locator));
  129. exist = true;
  130. return exist;
  131. } catch (NoSuchElementException e) {
  132. this.logger.error(e);
  133. }
  134.  
  135. /* name */
  136. try {
  137. webdriver.findElement(By.name(locator));
  138. exist = true;
  139. return exist;
  140. } catch (NoSuchElementException e) {
  141. this.logger.error(e);
  142. }
  143.  
  144. /* cssSelector */
  145. try {
  146. webdriver.findElement(By.cssSelector(locator));
  147. exist = true;
  148. return exist;
  149. } catch (NoSuchElementException e) {
  150. this.logger.error(e);
  151. }
  152.  
  153. /* xpath */
  154. try {
  155. webdriver.findElement(By.xpath(locator));
  156. exist = true;
  157. return exist;
  158. } catch (NoSuchElementException e) {
  159. this.logger.error(e);
  160. }
  161.  
  162. /* linkText */
  163. try {
  164. webdriver.findElement(By.linkText(locator));
  165. exist = true;
  166. return exist;
  167. } catch (NoSuchElementException e) {
  168. this.logger.error(e);
  169. }
  170.  
  171. /* className */
  172. try {
  173. webdriver.findElement(By.className(locator));
  174. exist = true;
  175. return exist;
  176. } catch (NoSuchElementException e) {
  177. this.logger.error(e);
  178. }
  179.  
  180. /* partialLinkText */
  181. try {
  182. webdriver.findElement(By.partialLinkText(locator));
  183. exist = true;
  184. return exist;
  185. } catch (NoSuchElementException e) {
  186. this.logger.error(e);
  187. }
  188.  
  189. /* tagName */
  190. try {
  191. webdriver.findElement(By.tagName(locator));
  192. exist = true;
  193. return exist;
  194. } catch (NoSuchElementException e) {
  195. this.logger.error(e);
  196. }
  197.  
  198. return exist;
  199. }
  200.  
  201. /**
  202. * @function Verify the element exist or not
  203. *
  204. * @author Aaron.ffp
  205. * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java isElementExistByXpath, 2014-11-23 4:03:52 Exp $
  206. *
  207. * @param webdriver : WebDriver
  208. * @param locator : XPath
  209. *
  210. * @return boolean
  211. */
  212. public boolean isElementExistByXpath(WebDriver webdriver, String locator){
  213. boolean isExists = false;
  214.  
  215. try {
  216. webdriver.findElement(By.xpath(locator));
  217. isExists = true;
  218. } catch (NoSuchElementException nsee) {
  219. this.logger.error(nsee);
  220. nsee.printStackTrace();
  221. }
  222.  
  223. return isExists;
  224. }
  225.  
  226. /**
  227. * @function Verify the element exist or not
  228. *
  229. * @author Aaron.ffp
  230. * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java isWebelementExistByXpath, 2014-11-23 4:03:52 Exp $
  231. *
  232. * @param locator : XPath
  233. * @return boolean
  234. */
  235. public boolean isElementExistByXpath(String locator){
  236. boolean isExists = false;
  237.  
  238. try {
  239. this.webdriver.findElement(By.xpath(locator));
  240. isExists = true;
  241. } catch (NoSuchElementException nsee) {
  242. this.logger.error(nsee);
  243. nsee.printStackTrace();
  244. }
  245.  
  246. return isExists;
  247. }

PS:当元素不可用或者隐藏式,返回的也是不存在,请知悉!

至此,WebUI 自动化功能测试脚本第 027-判断元素是否存在 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

Selenium2学习-029-WebUI自动化实战实例-027-判断元素是否存在的更多相关文章

  1. Selenium2学习-012-WebUI自动化实战实例-010-解决元素失效:StaleElementReferenceException: stale element reference: element is not attached to the page document

    元素失效的想象提示信息如下图所示,此种问题通常是因为元素页面刷新之后,为重新获取元素导致的. 解决此类问题比较简单,只需要在页面刷新之后,重新获取一下元素,就可以消除此种错误了. 以下以易迅网搜索为例 ...

  2. Selenium2学习-030-WebUI自动化实战实例-028-获取元素位置及大小

    自动化测试过程中,有时需要获取元素的位置.大小,以获取元素的位置,通过 Actions 模拟鼠标,进行相对坐标操作.例如,有些元素定位不方便,或者需要对某一元素相对区域范围进行暴力点击测试,此时就需要 ...

  3. Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置

    此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...

  4. Selenium2学习-007-WebUI自动化实战实例-005-解决 Firefox 版本不兼容:org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary

    此文主要讲述 Java 运行 Selenium 脚本时,因 Friefox 浏览器版本与 selenium-server-standalone-x.xx.x.jar 不兼容引起的 org.openqa ...

  5. Selenium2学习-035-WebUI自动化实战实例-033-页面快照截图应用之三 -- 区域截图(专业版)

    之前有写过两篇博文讲述了 WebUI 自动化测试脚本中常用的截图方法,敬请参阅如下所示链接: 浏览器显示区域截图 浏览器指定区域截图 那么当需要截取的区域不在浏览器显示窗口范围之内时,之前的方法显然无 ...

  6. Selenium2学习-027-WebUI自动化实战实例-025-JavaScript 在 Selenium 自动化中的应用实例之三(页面滚屏,模拟鼠标拖动滚动条)

    日常的 Web UI 自动化测试过程中,get 或 navigate 到指定的页面后,若想截图的元素或者指定区域范围不在浏览器的显示区域内,则通过截屏则无法获取相应的信息,反而浪费了无畏的图片服务器资 ...

  7. Selenium2学习-039-WebUI自动化实战实例-文件上传下载

    通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...

  8. Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题

    日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...

  9. Selenium2学习-016-WebUI自动化实战实例-014-Selenium 窗口选择

    在日常的 WebUI 自动化测试脚本编写过程中,经常需要打开新的页面,或者在多个打开的页面之间进行切换,以对页面元素进行相应的操作,以模拟用户的行为,实现 UI 的自动化测试.在过往的时间中,经常有初 ...

随机推荐

  1. Java_获取当前月最后一天

    List<String> ms = DateUtils.getMonths(7,"yyyyMM"); SimpleDateFormat sdf = new Simple ...

  2. Java JDBC批处理插入数据操作

    在此笔记里,我们将看到我们如何可以使用像Statement和PreparedStatement JDBC API来批量在任何数据库中插入数据.此外,我们将努力探索一些场景,如在内存不足时正常运行,以及 ...

  3. C# 使用 GetOleDbSchemaTable 检索架构信息(表、列、主键等)

    本文演示如何用 ADO.NET 中 OleDbConnection 对象的 GetOleDbSchemaTable 方法检索数据库架构信息.数据源中的架构信息包括数据库或可通过数据库中的数据源.表和视 ...

  4. 【新产品发布】《GM1001 4~20mA 高精度电流采集模块》

    一.主要特性 1.测量精度高达±0.01%FS±0.002mA: 2.采样电阻仅10欧姆(20mA时压降仅0.2V),对被测系统影响  微乎其微: 3.差分输入,可测量正反电流无需改动硬件,使用方便: ...

  5. 字体 font typograph

    https://en.wikipedia.org/wiki/Typeface *** 字体渲染 https://www.smashingmagazine.com/2012/04/a-closer-lo ...

  6. HDU 2089 数位dp入门

    开始学习数位dp...一道昨天看过代码思想的题今天打了近两个小时..最后还是看了别人的代码找bug...(丢丢) 传说院赛要取消 ? ... 这么菜不出去丢人也好吧~ #include<stdi ...

  7. PHP 错误与异常 笔记与总结(3)PHP 配置文件(php.ini)中与错误相关的选项 与 设置错误级别

    [PHP 配置文件中与错误相关的选项 ] 选项 描述 error_reporting 设置错误报告的级别 display_errors 是否显示错误 log_errors 设置是否将错误信息记录到日志 ...

  8. ThinkPHP 关联模型中查询某条记录的父级(非查询子级)

    数据表 id      cat_name      cat_pid 76     手机.数码     0 84     手机配件        76 86     蓝牙耳机        84 从属关 ...

  9. MySQL存储引擎总结

    MySQL存储引擎总结 作者:果冻想 字体:[增加 减小] 类型:转载   这篇文章主要介绍了MySQL存储引擎总结,本文讲解了什么是存储引擎.MyISAM.InnoDB.MEMORY.MERGE等内 ...

  10. PHP Error 和 Logging 函数

    PHP Error 和 Logging 函数 PHP Error 和 Logging 简介 Error 和 Logging 函数允许您对错误进行处理和记录. Error 函数允许用户定义错误处理规则, ...