在自动化脚本编写过程中,经常需要获取页面元素的文本进行判断,以便对于不同的文本进行不同的处理。比如:很多的购物网站,加入购物车的按钮是有多个状态的(加入购物车、到货通知、暂不销售等),那么在实际的操作过程中,需要对此按钮对应的不同的值,执行相应的逻辑。
代码相对比较简单,在此不再详细说明了,直接上码,敬请各位小主参阅,若有不足之处,敬请大神指正,非常感谢!
获取元素值的源码如下所示:
/**
* @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 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^