非常简单的方法封装,就不啰嗦了,直接上码咯 ^_^

     /**
* Get element. It will be return null when there is not such element.
*
* @author Aaron.ffp
* @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getWebElement, 2015-7-31 13:56:59 Exp $
*
* @param by : By
*
* @return WebElement
*/
public WebElement getElement(By by){
try {
return this.webdriver.findElement(by);
} catch (Exception e) {
return null;
}
} /**
* Get element by locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java getElement, 2015-1-22 3:15:57 Exp $
*
* @param locator : the expression of locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @return WebElement
*/
public WebElement getElement(String locator){
WebElement webelement = null; /* by ID */
try {
return this.webdriver.findElement(By.id(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by name */
try {
return this.webdriver.findElement(By.name(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by xpath */
try {
return this.webdriver.findElement(By.xpath(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by cssSelector */
try {
return this.webdriver.findElement(By.cssSelector(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by linkText */
try {
return this.webdriver.findElement(By.linkText(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by className */
try {
return this.webdriver.findElement(By.className(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by partialLinkText */
try {
return this.webdriver.findElement(By.partialLinkText(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by tagName */
try {
return this.webdriver.findElement(By.tagName(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} return webelement;
} /**
* Get element by locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java getElement, 2015-1-22 3:15:57 Exp $
*
* @param webdriver : WebDriver
* @param locator : the expression of locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @return WebElement
*/
public WebElement getElement(WebDriver webdriver, String locator){
WebElement webelement = null; /* by ID */
try {
return webdriver.findElement(By.id(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by name */
try {
return webdriver.findElement(By.name(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by xpath */
try {
return webdriver.findElement(By.xpath(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by cssSelector */
try {
return webdriver.findElement(By.cssSelector(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by linkText */
try {
return webdriver.findElement(By.linkText(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by className */
try {
return webdriver.findElement(By.className(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by partialLinkText */
try {
return webdriver.findElement(By.partialLinkText(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} /* by tagName */
try {
return webdriver.findElement(By.tagName(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
} return webelement;
}

至此,WebUI 自动化功能测试脚本第 024-获取页面元素 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

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

Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素的更多相关文章

  1. Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值

    在自动化脚本编写过程中,经常需要获取页面元素的文本进行判断,以便对于不同的文本进行不同的处理.比如:很多的购物网站,加入购物车的按钮是有多个状态的(加入购物车.到货通知.暂不销售等),那么在实际的操作 ...

  2. Selenium2学习-009-WebUI自动化实战实例-007-Selenium 8种元素定位实战实例源代码(百度首页搜索录入框及登录链接)

    此 文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,通过 ID.name.xpath.cssSelector.linkText.className.partialLinkTe ...

  3. Selenium2学习-034-WebUI自动化实战实例-032-获取页面 body 大小

    获取 body 元素大小的方法,非常简单,直接上码,敬请参阅! /** * Get body size * * @author Aaron.ffp * @version V1.0.0: autoSel ...

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

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

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

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

  6. 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 ...

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

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

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

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

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

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

随机推荐

  1. 【BZOJ】1452: [JSOI2009]Count

    http://www.lydsy.com/JudgeOnline/problem.php?id=1452 题意:n×m的矩阵上每个点有个颜色,现在有q个操作:1 x y c 将点(x,y)的颜色改为c ...

  2. 【BZOJ】1003: [ZJOI2006]物流运输trans(SPFA+DP)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1003 这题一开始看是不会的额,,,还是看题解了..一开始我觉得不能用最短路啥的,,看了题解发现这是d ...

  3. oracle系列--第二篇 oracle下载

    对于很多新手来说,包括我之前也是这样,知道oracle数据库,但是就是不知道在哪里下载.有时候,上到oracle官方网站上面都找不到下载的地方. 这不像apache里面那么直接,我们想下载如:tomc ...

  4. 用MyEclipse搭建SSH框架(Struts2 Spring Hibernate)

    1.new一个web project. 2.右键项目,为项目添加Struts支持. 点击Finish.src目录下多了struts.xml配置文件. 3.使用MyEclipse DataBase Ex ...

  5. [转] - 经典SQL语句大全

    经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...

  6. 用openGL实现用黑白相间的棋盘图案填充多边形

    #include<gl/glut.h> #include<windows.h> ; ,b0=,a1=,b1=,a2=,b2=,a3=,b3=; ,winHeight=; voi ...

  7. nodejs 更新最新版本

    sudo npm cache clean -f sudo npm install -g n sudo n stable

  8. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. HTTP 笔记与总结(6)referer 头与防盗链

    在百度贴吧(或 QQ 空间等)中找到一张图片,复制图片地址,在站外通过 img src 引用,会发现: 此外,在一些统计软件中,统计访客的来路(直接访问.外部链接.搜索引擎),都用到了 HTTP 协议 ...

  10. PHP 设计模式 笔记与总结(10)数据对象映射模式 2

    [例2]数据对象映射模式结合[工厂模式]和[注册模式]的使用. 入口文件 index.php: <?php define('BASEDIR',__DIR__); //定义根目录常量 includ ...