zoukankan      html  css  js  c++  java
  • Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值

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

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

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

      1     /**
      2      * @function Get text of element. It will be return null when the element not exist or By is null
      3      * 
      4      * @author Aaron.ffp
      5      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:48:04 Exp $
      6      * 
      7      * @param by  : By
      8      * 
      9      * @return String
     10      */
     11     public String getElementText(By by){
     12         String elementText = "";
     13         
     14         if (by == null) {
     15             return "";
     16         }
     17         
     18         try {
     19             elementText = this.getElement(by).getText().toString().trim();
     20         } catch (NoSuchElementException nsee) {
     21             this.logger.error(nsee);
     22             nsee.printStackTrace();
     23         }
     24         
     25         return elementText;
     26     }
     27     
     28     /**
     29      * @function Get text of element. It will be return null when the element not exist or locator is empty
     30      * 
     31      * @author Aaron.ffp
     32      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:47:40 Exp $
     33      * 
     34      * @param locator  : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
     35      * 
     36      * @return String
     37      */
     38     public String getElementText(String locator){
     39         String elementText = "";
     40         
     41         locator = ((locator == null)|| "".equals(locator)) ? "" : locator;
     42         
     43         try {
     44             elementText = this.getElement(locator).getText().toString().trim();
     45         } catch (NoSuchElementException nsee) {
     46             this.logger.error(nsee);
     47             nsee.printStackTrace();
     48         }
     49         
     50         return elementText;
     51     }
     52     
     53     /**
     54      * @function Get text of element. It will be return empty when the element not exist or locator is empty
     55      * 
     56      * @author Aaron.ffp
     57      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 19:10:42 Exp $
     58      * 
     59      * @param webdriver : WebDriver
     60      * @param by        : By
     61      * 
     62      * @return String
     63      */
     64     public String getElementText(WebDriver webdriver, By by){
     65         String elementText = "";
     66         
     67         try {
     68             elementText = this.getElement(webdriver, by).getText().toString().trim();
     69         } catch (NoSuchElementException nsee) {
     70             this.logger.error(nsee);
     71             nsee.printStackTrace();
     72         }
     73         
     74         return elementText;
     75     }
     76     
     77     /**
     78      * @function Get text of element. It will be return empty when the element not exist or locator is empty
     79      * 
     80      * @author Aaron.ffp
     81      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 19:01:03 Exp $
     82      * 
     83      * @param webdriver : WebDriver
     84      * @param locator   : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
     85      * 
     86      * @return String
     87      */
     88     public String getElementText(WebDriver webdriver, String locator){
     89         String elementText = "";
     90         
     91         try {
     92             elementText = this.getElement(webdriver, locator).getText().toString().trim();
     93         } catch (NoSuchElementException nsee) {
     94             this.logger.error(nsee);
     95             nsee.printStackTrace();
     96         }
     97         
     98         return elementText;
     99     }
    100     
    101     /**
    102      * @function Get text of element. It will be return empty when the element not exist or locator is empty
    103      * 
    104      * @author Aaron.ffp
    105      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementTextByXpath, 2015-1-25 20:47:03 Exp $
    106      * 
    107      * @param locator : XPath
    108      * 
    109      * @return String
    110      */
    111     public String getElementTextByXpath(String locator){
    112         String elementText = "";
    113         
    114         try {
    115             elementText = this.webdriver.findElement(By.xpath(locator)).getText().toString().trim();
    116         } catch (NoSuchElementException nsee) {
    117             this.logger.error(nsee);
    118             nsee.printStackTrace();
    119         }
    120         
    121         return elementText;
    122     }
    123     
    124     /**
    125      * @function Get text of element. It will be return empty when the element not exist or locator is empty
    126      * 
    127      * @author Aaron.ffp
    128      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:47:03 Exp $
    129      * 
    130      * @param webdriver : WebDriver
    131      * @param locator   : XPath
    132      * 
    133      * @return String
    134      */
    135     public String getElementTextByXpath(WebDriver webdriver, String locator){
    136         String elementText = "";
    137         
    138         try {
    139             elementText = this.webdriver.findElement(By.xpath(locator)).getText().toString().trim();
    140         } catch (NoSuchElementException nsee) {
    141             this.logger.error(nsee);
    142             nsee.printStackTrace();
    143         }
    144         
    145         return elementText;
    146     }

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

      1     /**
      2      * @function Get text of attribute to the element. It will be return empty when the attribute not exist.
      3      * 
      4      * @author Aaron.ffp
      5      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 20:42:13 Exp $
      6      * 
      7      * @param locator   : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
      8      * @param attribute : attribute of element
      9      * 
     10      * @return String
     11      */
     12     public String getElementValue(By by, String attribute){
     13         String attrbuteValue = "";
     14         
     15         try {
     16             attrbuteValue = this.getElement(by).getAttribute(attribute);
     17         } catch (NoSuchElementException nsee) {
     18             this.logger.error(nsee);
     19             nsee.printStackTrace();
     20         }
     21         
     22         return attrbuteValue;
     23     }
     24     
     25     /**
     26      * @function Get text of attribute to the element. It will be return empty when the attribute not exist.
     27      * 
     28      * @author Aaron.ffp
     29      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 20:37:02 Exp $
     30      * 
     31      * @param locator   : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
     32      * @param attribute : attribute of element
     33      * 
     34      * @return String
     35      */
     36     public String getElementValue(String locator, String attribute){
     37         String attrbuteValue = "";
     38         
     39         try {
     40             attrbuteValue = this.getElement(locator).getAttribute(attribute);
     41         } catch (NoSuchElementException nsee) {
     42             this.logger.error(nsee);
     43             nsee.printStackTrace();
     44         }
     45         
     46         return attrbuteValue;
     47     }
     48     
     49     /**
     50      * @function Get text of attribute to the element. It will be return empty when the attribute not exist.
     51      * 
     52      * @author Aaron.ffp
     53      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 19:41:17 Exp $
     54      * 
     55      * @param webdriver : WebDriver
     56      * @param locator   : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
     57      * @param attribute : attribute of element
     58      * 
     59      * @return String
     60      */
     61     public String getElementValue(WebDriver webdriver, By by, String attribute){
     62         String attrbuteValue = "";
     63         
     64         try {
     65             attrbuteValue = this.getElement(webdriver, by).getAttribute(attribute);
     66         } catch (NoSuchElementException nsee) {
     67             this.logger.error(nsee);
     68             nsee.printStackTrace();
     69         }
     70         
     71         return attrbuteValue;
     72     }
     73     
     74     /**
     75      * @function Get text of attribute to the element. It will be return empty when the attribute not exist.
     76      * 
     77      * @author Aaron.ffp
     78      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 19:36:23 Exp $
     79      * 
     80      * @param webdriver : WebDriver
     81      * @param locator   : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
     82      * @param attribute : attribute of element
     83      * 
     84      * @return String
     85      */
     86     public String getElementValue(WebDriver webdriver, String locator, String attribute){
     87         String attrbuteValue = "";
     88         
     89         try {
     90             attrbuteValue = this.getElement(webdriver, locator).getAttribute(attribute);
     91         } catch (NoSuchElementException nsee) {
     92             this.logger.error(nsee);
     93             nsee.printStackTrace();
     94         }
     95         
     96         return attrbuteValue;
     97     }
     98     
     99     /**
    100      * @function Get text of attribute to the element. It will be return empty when the attribute not exist.
    101      * 
    102      * @author Aaron.ffp
    103      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValueByXpath, 2015-1-25 20:47:03 Exp $
    104      * 
    105      * @param locator   : XPath
    106      * @param attribute : attribute of element
    107      * 
    108      * @return String
    109      */
    110     public String getElementValueByXpath(String locator, String attribute){
    111         String attrbuteValue = "";
    112         
    113         try {
    114             attrbuteValue = this.webdriver.findElement(By.xpath(locator)).getAttribute(attribute);
    115         } catch (NoSuchElementException nsee) {
    116             this.logger.error(nsee);
    117             nsee.printStackTrace();
    118         }
    119         
    120         return attrbuteValue;
    121     }
    122     
    123     /**
    124      * @function Get text of attribute to the element. It will be return empty when the attribute not exist.
    125      * 
    126      * @author Aaron.ffp
    127      * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValueByXpath, 2015-1-25 20:47:03 Exp $
    128      * 
    129      * @param webdriver : WebDriver
    130      * @param locator   : XPath
    131      * @param attribute : attribute of element
    132      * 
    133      * @return String
    134      */
    135     public String getElementValueByXpath(WebDriver webdriver, String locator, String attribute){
    136         String attrbuteValue = "";
    137         
    138         try {
    139             attrbuteValue = this.webdriver.findElement(By.xpath(locator)).getAttribute(attribute);
    140         } catch (NoSuchElementException nsee) {
    141             this.logger.error(nsee);
    142             nsee.printStackTrace();
    143         }
    144         
    145         return attrbuteValue;
    146     }

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

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

  • 相关阅读:
    Linux KVM
    linux 下添加,修改,删除路由
    爬虫之requests urllib urllib2 BeautifulSoup
    MyBatis-Plus+mysql5.7 动态拼接sql语句 分页查询 自定义sql 查询条件 分组 排序
    springboot2.x+neo4j创建图形数据库
    linux(centos7)下springboot项目jar包使用shell脚本运行
    java网络爬虫-利用phantomjs和jsoup爬取动态ajax加载页面
    springboot开发qq第三方授权登录
    linux(centos7)下nginx访问静态文件403错误解决过程
    idea2019.2+springboot2.2.1版本配置热部署
  • 原文地址:https://www.cnblogs.com/fengpingfan/p/4699568.html
Copyright © 2011-2022 走看看