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

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

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

     /**
* @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. EnableViewState=“false”不能乱用啊

    有时候页面源文件里有一段看上去像乱码的代码,这时候为了加快页面的加载速度,可以使用EnableViewState=“false”,这时候页面上的乱码就会消失了.但是,关于这个问题作者郁闷了好久,之前为 ...

  2. 运用正则表达式在Asp中过滤Html标签代码的四种不同方法

    Function RemoveHTML(strHTML)Dim objregExp, Match, MatchesSet objRegExp = New RegexpobjRegExp.IgnoreC ...

  3. jQuery.Validate验证库详解

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  4. [LintCode] Implement Trie 实现字典树

    Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...

  5. http页面转发和重定向的区别

    一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下:request.getRequestDispatcher("new.jsp").forward(request ...

  6. STATIC::含义

    Static 关键字,是作为作用域引用,类似Parent和self 关键字,和Parent和 Self不同 Parent引用父类作用域 Self 引用当前类作用域 Static 引用全部静态作用于,子 ...

  7. Sql Server 常用方法、存储过程备用

    常用方法 --字符串转换成数字 --CAST("1" AS int) --CONVERT(int,"1") --截取字符串 SUBSTRING(OccurreA ...

  8. linux系统

    ● ubuntu提示"sudo: unable to resolve host volcano: No such file or directory" 修改/etc/hosts,在 ...

  9. PHP fwrite() 函数与 file_put_contents() 函数的比较

    两个 PHP 函数都可以把字符串保存到文件中,fwrite() 函数的格式是: int fwrite ( resource handle , string string [ , int length] ...

  10. PHP正则表达式及实例

    PHP正则表达式及实例 博客分类: Php / Pear / Mysql / Node.js 正则表达式PHPWordPressFPApache  关联: 正则表达式 去除连续空白 + 获取url + ...