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

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

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

     /**
* @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语言】13-指针和字符串

    字符串回顾 一个字符串由一个或多个字符组成,因此我们可以用字符数组来存放字符串,不过在数组的尾部要加上一个空字符'\0'. char s[] = "李洪强"; 上面的代码定义了一个 ...

  2. [Bug FIX]安装 account_check_writing模块后采购收据打印报错的问题

    大写金额没填报错 修改:report_check.xml文件,把<span t-esc="fill_stars(o.amount_in_word)"/>一行替换为 &l ...

  3. Function Scope

    JavaScript’s function scope means that all variables declared within a function are visi-ble through ...

  4. nginx不支持pathinfo函数

    server { listen ; server_name www.domain.com domain.com; error_page /.html; error_page /50x.html; lo ...

  5. 语艺杂谈1 – MAP赋值与插入

    MAP赋值和插入,对于相同ID的处理方式不同,前者为替换 后者为插入失败 #include <map> #include <string> #include <iostr ...

  6. Apche Kafka 的生与死 – failover 机制详解

    Kafka 作为 high throughput 的消息中间件,以其性能,简单和稳定性,成为当前实时流处理框架中的主流的基础组件. 当然在使用 Kafka 中也碰到不少问题,尤其是 failover ...

  7. cookie 操作

    //创建并赋值 重新赋值也是这样操作 document.cookie="userId=828"; document.cookie="userName=hulk" ...

  8. java笔记--关于线程同步(5种同步方式)【转】

    为何要使用同步?     java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查),      将会导致数据不准确,相互之间产生冲突,因此加入同步锁以避免在该线程没有完 ...

  9. PHP其它常用函数;<<<面向对象(OPP)的三大特性:封装、继承、加态:>>> <----面试题 ;构造方法、析构方法,魔术方法、set、get方法;静态;抽象类;接口

    PHP其它常用函数:     赋值:$r->name = "元素";      取值: echo $r->name;  count()   计算数组中的元素数目或对象中 ...

  10. 低功耗蓝牙4.0BLE编程-nrf51822开发(3)

    蓝牙协议栈 nrf51822开发中,蓝牙协议栈和应用开发是分开的. (1)兼容蓝牙4.0低功耗协议栈基带层,L2CAP\AAT\SM\GAP\GATT协议,设备和广播,GATT客户端和服务器,SMP支 ...