Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值
在自动化脚本编写过程中,经常需要获取页面元素的文本进行判断,以便对于不同的文本进行不同的处理。比如:很多的购物网站,加入购物车的按钮是有多个状态的(加入购物车、到货通知、暂不销售等),那么在实际的操作过程中,需要对此按钮对应的不同的值,执行相应的逻辑。
代码相对比较简单,在此不再详细说明了,直接上码,敬请各位小主参阅,若有不足之处,敬请大神指正,非常感谢!
获取元素值的源码如下所示:
/**
* @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-获取页面元素值或者元素属性值的更多相关文章
- Selenium2学习-034-WebUI自动化实战实例-032-获取页面 body 大小
获取 body 元素大小的方法,非常简单,直接上码,敬请参阅! /** * Get body size * * @author Aaron.ffp * @version V1.0.0: autoSel ...
- Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素
非常简单的方法封装,就不啰嗦了,直接上码咯 ^_^ /** * Get element. It will be return null when there is not such element. ...
- Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置
此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...
- Selenium2学习-035-WebUI自动化实战实例-033-页面快照截图应用之三 -- 区域截图(专业版)
之前有写过两篇博文讲述了 WebUI 自动化测试脚本中常用的截图方法,敬请参阅如下所示链接: 浏览器显示区域截图 浏览器指定区域截图 那么当需要截取的区域不在浏览器显示窗口范围之内时,之前的方法显然无 ...
- 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 ...
- Selenium2学习-027-WebUI自动化实战实例-025-JavaScript 在 Selenium 自动化中的应用实例之三(页面滚屏,模拟鼠标拖动滚动条)
日常的 Web UI 自动化测试过程中,get 或 navigate 到指定的页面后,若想截图的元素或者指定区域范围不在浏览器的显示区域内,则通过截屏则无法获取相应的信息,反而浪费了无畏的图片服务器资 ...
- Selenium2学习-039-WebUI自动化实战实例-文件上传下载
通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...
- Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题
日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...
- Selenium2学习-016-WebUI自动化实战实例-014-Selenium 窗口选择
在日常的 WebUI 自动化测试脚本编写过程中,经常需要打开新的页面,或者在多个打开的页面之间进行切换,以对页面元素进行相应的操作,以模拟用户的行为,实现 UI 的自动化测试.在过往的时间中,经常有初 ...
随机推荐
- ios cocos2d FPS过低的解决方法
每当运行程序时,左下角的FPS就低到了10,使app很卡, 原来程序主要卡的部分 -(void)draw{ NSDate *startTime = [NSDate date]; [self func] ...
- OI分类
黑字:认识 红字:要学 未添加:要学 ├─模拟├─字符串│ ├─字符串基础│ ├─manacher│ ├─kmp│ ├─trie│ ├─ac自动机│ ├─后缀数组( ...
- eWebeditor编辑器上传图片路径错误解决方法[疑难杂症]【转,作者:unvs】
做了一个多版本的网站,后台用的编辑器是eWebeditor,NET版,后面发现上传图片或者文件之后,路径错误无法显示,必须手工修改才行.. 为了更清楚的说明问题,我下面会说的比较详细,首先是网站文件框 ...
- NBUT 1525 Cow Xor(01字典树+前缀思想)
[1525] Cow Xor 时间限制: 2000 ms 内存限制: 65535 K 问题描述 农民约翰在喂奶牛的时候被另一个问题卡住了.他的所有N(1 <= N <= 100,000)个 ...
- Mybatis分页和Spring的集成
写了一个Mybatis分页控件,在这记录一下使用方式. 在Maven中加入依赖: ? 1 2 3 4 5 6 7 8 9 <dependencies> ... <depe ...
- DS实验题 Inversion
题目: 解题过程: 第一次做这题的时候,很自然的想到了冒泡和选择,我交的代码是用选择写的.基本全WA(摊手). 贴上第一次的代码: // // main.cpp // sequenceschange ...
- PHP 错误与异常 笔记与总结(9)自定义错误处理器
自定义错误处理器更加智能. <?php class myErrorHandler{ //$message:错误信息 //$filename:错误文件名 //$line:错误行号 //$vars: ...
- cURL 学习笔记与总结(1)概念
概念: cURL(Client URL Library Functions)is a command line tool for transfering data with URL syntax(使用 ...
- json解析json字符串时候,数组必须对应jsonObjectArray,不能对应JsonObject。否则会解析错误。
json第三方解析json字符串时候,json数组必须对应jsonObjectArray,不能对应JsonObject.->只要是[]开头的都是json数组字符串,就要用jsonArray解析 ...
- 个人翻译的cedec2010基于物理的光照
作为自己介绍基于物理渲染计划的一部分,在自己总结和发布的同时,也会翻译一些国外的优秀资料做推广 本文是Tri Ace 在 cedec2010上发布的文章,主要描述了他们基于物理光照的实现方法,这 ...