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

     /**
* 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. TYVJ P1067 合唱队形 Label:上升子序列?

    背景 NOIP2004 提高组 第三道 描述     N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号 ...

  2. 关于实现banner轮换的问题,如何修改

    最近遇到了这样的问题,本来banner都是gif格式的,但是现在要求上传图片格式为jpg时,运用JS实现动画效果,原来的也能用. aspx: <div id="bh" run ...

  3. MySQL 用户管理——权限表

    权限表 权限表存放在mysql数据库中 user表结构 用户列:Host.User.Password 权限列:*priv 资源控制列:max* 安全列:其余   db表 存储了用户对某个数据库的操作权 ...

  4. html5调用手机摄像头,实现拍照上传功能

    今天做手机网站,想实现手机扫描二维码功能.首先实现在浏览器中调用手机摄像头,实现拍照功能并且把拍下的照片显示在页面并上传到服务器上,然后再在服务器端进行分析. 首先实现在浏览器中调用摄像头,当然用现在 ...

  5. 字符串中的一些基本操作函数(c语言)

    其中很多函数返回的都是首地址,程序中只是将该地址后的内容全部输出来了...并没有作特殊处理输出地址...还有几个函数有点小bug. #include"iostream" #incl ...

  6. MongoDB数据备份与恢复

    测试环境:windows 一. 导出数据F:\DbSoft\soft\master\bin>mongoexport /h 127.0.0.1 /port 50000 /d testdb /c t ...

  7. 2016.04.28,英语,《Vocabulary Builder》Unit 20

    nom, comes from the Latin word for 'name'. nominee is 'named', [ˌnɑːmɪ'niː] n. 被提名的人, 名义人. binomial ...

  8. A trip through the Graphics Pipeline 2011_01

    It’s been awhile since I posted something here, and I figured I might use this spot to explain some ...

  9. python twisted启动定时服务

    以下是python脚本send_mms.py #############################################!/usr/bin/python# -*- coding: ut ...

  10. Ubuntu 14.04 LTS 安装 VNC Viewer

    1.修改镜像源: /etc/apt/sources.list将"http://archive.ubuntu.com/ubuntu/"替换为: http://cn.archive.u ...