zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素

实例学习自动化 获取 实战 页面 元素 WebUI
2023-09-11 14:18:59 时间

非常简单的方法封装,就不啰嗦了,直接上码咯 ^_^

  1     /**
  2      * Get element. It will be return null when there is not such element.
  3      * 
  4      * @author Aaron.ffp
  5      * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getWebElement, 2015-7-31 13:56:59 Exp $
  6      * 
  7      * @param by : By
  8      * 
  9      * @return WebElement
 10      */
 11     public WebElement getElement(By by){
 12         try {
 13             return this.webdriver.findElement(by);
 14         } catch (Exception e) {
 15             return null;
 16         }
 17     }
 18 
 19     /**
 20      * Get element by locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
 21      * 
 22      * @author Aaron.ffp
 23      * @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java getElement, 2015-1-22 3:15:57 Exp $
 24      * 
 25      * @param locator  : the expression of locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
 26      * 
 27      * @return WebElement
 28      */
 29     public WebElement getElement(String locator){
 30         WebElement webelement = null;
 31         
 32         /* by ID */
 33         try {
 34             return this.webdriver.findElement(By.id(locator));
 35         } catch (NoSuchElementException e) {
 36             this.logger.error(e);
 37             webelement = null;
 38         }
 39         
 40         /* by name */
 41         try {
 42             return this.webdriver.findElement(By.name(locator));
 43         } catch (NoSuchElementException e) {
 44             this.logger.error(e);
 45             webelement = null;
 46         }
 47         
 48         /* by xpath */
 49         try {
 50             return this.webdriver.findElement(By.xpath(locator));
 51         } catch (NoSuchElementException e) {
 52             this.logger.error(e);
 53             webelement = null;
 54         }
 55         
 56         /* by cssSelector */
 57         try {
 58             return this.webdriver.findElement(By.cssSelector(locator));
 59         } catch (NoSuchElementException e) {
 60             this.logger.error(e);
 61             webelement = null;
 62         }
 63         
 64         /* by linkText */
 65         try {
 66             return this.webdriver.findElement(By.linkText(locator));
 67         } catch (NoSuchElementException e) {
 68             this.logger.error(e);
 69             webelement = null;
 70         }
 71         
 72         /* by className */
 73         try {
 74             return this.webdriver.findElement(By.className(locator));
 75         } catch (NoSuchElementException e) {
 76             this.logger.error(e);
 77             webelement = null;
 78         }
 79         
 80         /* by partialLinkText */
 81         try {
 82             return this.webdriver.findElement(By.partialLinkText(locator));
 83         } catch (NoSuchElementException e) {
 84             this.logger.error(e);
 85             webelement = null;
 86         }
 87         
 88         /* by tagName */
 89         try {
 90             return this.webdriver.findElement(By.tagName(locator));
 91         } catch (NoSuchElementException e) {
 92             this.logger.error(e);
 93             webelement = null;
 94         }
 95         
 96         return webelement;
 97     }
 98     
 99     /**
100      * Get element by locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
101      * 
102      * @author Aaron.ffp
103      * @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java getElement, 2015-1-22 3:15:57 Exp $
104      * 
105      * @param webdriver      : WebDriver
106      * @param locator        : the expression of locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
107      * 
108      * @return WebElement
109      */
110     public WebElement getElement(WebDriver webdriver, String locator){
111         WebElement webelement = null;
112         
113         /* by ID */
114         try {
115             return webdriver.findElement(By.id(locator));
116         } catch (NoSuchElementException e) {
117             this.logger.error(e);
118             webelement = null;
119         }
120         
121         /* by name */
122         try {
123             return webdriver.findElement(By.name(locator));
124         } catch (NoSuchElementException e) {
125             this.logger.error(e);
126             webelement = null;
127         }
128         
129         /* by xpath */
130         try {
131             return webdriver.findElement(By.xpath(locator));
132         } catch (NoSuchElementException e) {
133             this.logger.error(e);
134             webelement = null;
135         }
136         
137         /* by cssSelector */
138         try {
139             return webdriver.findElement(By.cssSelector(locator));
140         } catch (NoSuchElementException e) {
141             this.logger.error(e);
142             webelement = null;
143         }
144         
145         /* by linkText */
146         try {
147             return webdriver.findElement(By.linkText(locator));
148         } catch (NoSuchElementException e) {
149             this.logger.error(e);
150             webelement = null;
151         }
152         
153         /* by className */
154         try {
155             return webdriver.findElement(By.className(locator));
156         } catch (NoSuchElementException e) {
157             this.logger.error(e);
158             webelement = null;
159         }
160         
161         /* by partialLinkText */
162         try {
163             return webdriver.findElement(By.partialLinkText(locator));
164         } catch (NoSuchElementException e) {
165             this.logger.error(e);
166             webelement = null;
167         }
168         
169         /* by tagName */
170         try {
171             return webdriver.findElement(By.tagName(locator));
172         } catch (NoSuchElementException e) {
173             this.logger.error(e);
174             webelement = null;
175         }
176         
177         return webelement;
178     }

 

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

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