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

代码相对比较简单,在此不再详细说明了,直接上码,敬请各位小主参阅,若有不足之处,敬请大神指正,非常感谢!

获取元素值的源码如下所示:

     /**
* @function Get text of element. It will be return null when the element not exist or By is null
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:48:04 Exp $
*
* @param by : By
*
* @return String
*/
public String getElementText(By by){
String elementText = ""; if (by == null) {
return "";
} try {
elementText = this.getElement(by).getText().toString().trim();
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return elementText;
} /**
* @function Get text of element. It will be return null when the element not exist or locator is empty
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:47:40 Exp $
*
* @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @return String
*/
public String getElementText(String locator){
String elementText = ""; locator = ((locator == null)|| "".equals(locator)) ? "" : locator; try {
elementText = this.getElement(locator).getText().toString().trim();
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return elementText;
} /**
* @function Get text of element. It will be return empty when the element not exist or locator is empty
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 19:10:42 Exp $
*
* @param webdriver : WebDriver
* @param by : By
*
* @return String
*/
public String getElementText(WebDriver webdriver, By by){
String elementText = ""; try {
elementText = this.getElement(webdriver, by).getText().toString().trim();
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return elementText;
} /**
* @function Get text of element. It will be return empty when the element not exist or locator is empty
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 19:01:03 Exp $
*
* @param webdriver : WebDriver
* @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @return String
*/
public String getElementText(WebDriver webdriver, String locator){
String elementText = ""; try {
elementText = this.getElement(webdriver, locator).getText().toString().trim();
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return elementText;
} /**
* @function Get text of element. It will be return empty when the element not exist or locator is empty
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementTextByXpath, 2015-1-25 20:47:03 Exp $
*
* @param locator : XPath
*
* @return String
*/
public String getElementTextByXpath(String locator){
String elementText = ""; try {
elementText = this.webdriver.findElement(By.xpath(locator)).getText().toString().trim();
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return elementText;
} /**
* @function Get text of element. It will be return empty when the element not exist or locator is empty
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:47:03 Exp $
*
* @param webdriver : WebDriver
* @param locator : XPath
*
* @return String
*/
public String getElementTextByXpath(WebDriver webdriver, String locator){
String elementText = ""; try {
elementText = this.webdriver.findElement(By.xpath(locator)).getText().toString().trim();
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return elementText;
}

获取元素属性值的源码如下所示:

     /**
* @function Get text of attribute to the element. It will be return empty when the attribute not exist.
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 20:42:13 Exp $
*
* @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
* @param attribute : attribute of element
*
* @return String
*/
public String getElementValue(By by, String attribute){
String attrbuteValue = ""; try {
attrbuteValue = this.getElement(by).getAttribute(attribute);
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return attrbuteValue;
} /**
* @function Get text of attribute to the element. It will be return empty when the attribute not exist.
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 20:37:02 Exp $
*
* @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
* @param attribute : attribute of element
*
* @return String
*/
public String getElementValue(String locator, String attribute){
String attrbuteValue = ""; try {
attrbuteValue = this.getElement(locator).getAttribute(attribute);
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return attrbuteValue;
} /**
* @function Get text of attribute to the element. It will be return empty when the attribute not exist.
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 19:41:17 Exp $
*
* @param webdriver : WebDriver
* @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
* @param attribute : attribute of element
*
* @return String
*/
public String getElementValue(WebDriver webdriver, By by, String attribute){
String attrbuteValue = ""; try {
attrbuteValue = this.getElement(webdriver, by).getAttribute(attribute);
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return attrbuteValue;
} /**
* @function Get text of attribute to the element. It will be return empty when the attribute not exist.
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 19:36:23 Exp $
*
* @param webdriver : WebDriver
* @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
* @param attribute : attribute of element
*
* @return String
*/
public String getElementValue(WebDriver webdriver, String locator, String attribute){
String attrbuteValue = ""; try {
attrbuteValue = this.getElement(webdriver, locator).getAttribute(attribute);
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return attrbuteValue;
} /**
* @function Get text of attribute to the element. It will be return empty when the attribute not exist.
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValueByXpath, 2015-1-25 20:47:03 Exp $
*
* @param locator : XPath
* @param attribute : attribute of element
*
* @return String
*/
public String getElementValueByXpath(String locator, String attribute){
String attrbuteValue = ""; try {
attrbuteValue = this.webdriver.findElement(By.xpath(locator)).getAttribute(attribute);
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return attrbuteValue;
} /**
* @function Get text of attribute to the element. It will be return empty when the attribute not exist.
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValueByXpath, 2015-1-25 20:47:03 Exp $
*
* @param webdriver : WebDriver
* @param locator : XPath
* @param attribute : attribute of element
*
* @return String
*/
public String getElementValueByXpath(WebDriver webdriver, String locator, String attribute){
String attrbuteValue = ""; try {
attrbuteValue = this.webdriver.findElement(By.xpath(locator)).getAttribute(attribute);
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return attrbuteValue;
}

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

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

Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值的更多相关文章

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

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

  2. Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素

    非常简单的方法封装,就不啰嗦了,直接上码咯 ^_^ /** * Get element. It will be return null when there is not such element. ...

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

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

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

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

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

  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. 李洪强-C语言1-指针

    C语言指针 前导程序   一.基本知识点 Int a=10; Int *p;//定义一个int类型的指针 P=&a;//指针变量p指向了变量a *p=20;//使用指针不通过变量直接修改变量a ...

  2. MatLab GUI Load .mat File 导入mat文件

    在MatLab中,我们用GUI时,有时候需要导入mat格式的图片,但是在GUI中调用load和在命令行里调用load不一样,在命令行里调用load('im.mat'),加载进去是uint8的矩阵,但是 ...

  3. html5文章 -- 使用 jQuery Mobile 与 HTML5 开发 Web App ——开发原则 | Kayo's Melody

    最近专注研究 jQuery Mobile —— 一款很方便就可以把 Web App 包装成适合 Android 与 iPhone 等触屏移动设备的 Javascript 库,结合 jQuery Mob ...

  4. cmd下常用的一些命令

    1.calc计算器 2.Mspaint画图 3.Netstat -anb查看端口 输入netstat -anb时可能会遇到下面问题 只要到搜索框输入cmd,然后到其快捷方式上右击以管理员身份运行即可 ...

  5. Self Service Password (SSP)

    安装SSP, 依赖包包括php5, php5-ldap, php5-mcrypt 启用mcrypt功能: sudo php5enmod mcrypt 第一部分: Apache 安装Apache, 并且 ...

  6. Bloomberg SEP 12.x 迁移小记

        备份 个文件: D:\Program Files\Symantec\Symantec Endpoint Protection Manager\Server Private Key Backup ...

  7. 【新产品发布】发布STM8S 核心板

    搞了一些STM8的核心板供大家把玩,先上几张图: 物品购买地址: http://item.taobao.com/item.htm?spm=686.1000925.1000774.17.5GMO5M&a ...

  8. JavaScript正则验证邮箱

    正则表达式/^正则$/.test() <html> <head> <title>JavaScript</title> <meta charset= ...

  9. NV Maxwell architecture

    按照NVIDIA的路线图来看,GTX 600以及GTX 700系列所采用的Kepler架构已经垂垂老矣,最早在明年第一季度,其继任者Maxwell架构可能就会和我们正式见面了.目前外媒已经放出了关于M ...

  10. 在UI线程之外显示Toast

    new Thread(){ public void run() { Looper.prepare(); Toast t = Toast.makeText(mContext, R.string.cras ...