非常简单的源码,敬请各位小主参阅。若有不足之处,敬请大神指正,不胜感激!
1 /** 2 * Verify the element exist or not 3 * 4 * @author Aaron.ffp 5 * @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $ 6 * 7 * @param by : By 8 * 9 * @return boolean 10 */ 11 public boolean isElementExist(By by){ 12 boolean exist = false; 13 14 try { 15 this.webdriver.findElement(by); 16 exist = true; 17 } catch (NoSuchElementException e) { 18 this.logger.error(e); 19 } 20 21 return exist; 22 } 23 24 /** 25 * Verify the element exist or not(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) 26 * 27 * @author Aaron.ffp 28 * @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $ 29 * 30 * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) 31 * 32 * @return boolean 33 */ 34 public boolean isElementExist(String locator){ 35 boolean exist = false; 36 37 /* ID */ 38 try { 39 this.webdriver.findElement(By.id(locator)); 40 exist = true; 41 return exist; 42 } catch (NoSuchElementException e) { 43 this.logger.error(e); 44 } 45 46 /* name */ 47 try { 48 this.webdriver.findElement(By.name(locator)); 49 exist = true; 50 return exist; 51 } catch (NoSuchElementException e) { 52 this.logger.error(e); 53 } 54 55 /* cssSelector */ 56 try { 57 this.webdriver.findElement(By.cssSelector(locator)); 58 exist = true; 59 return exist; 60 } catch (NoSuchElementException e) { 61 this.logger.error(e); 62 } 63 64 /* Xpath */ 65 try { 66 this.webdriver.findElement(By.xpath(locator)); 67 exist = true; 68 return exist; 69 } catch (NoSuchElementException e) { 70 this.logger.error(e); 71 } 72 73 /* linkText */ 74 try { 75 this.webdriver.findElement(By.linkText(locator)); 76 exist = true; 77 return exist; 78 } catch (NoSuchElementException e) { 79 this.logger.error(e); 80 } 81 82 /* className */ 83 try { 84 this.webdriver.findElement(By.className(locator)); 85 exist = true; 86 return exist; 87 } catch (NoSuchElementException e) { 88 this.logger.error(e); 89 } 90 91 /* partialLinkText */ 92 try { 93 this.webdriver.findElement(By.partialLinkText(locator)); 94 exist = true; 95 return exist; 96 } catch (NoSuchElementException e) { 97 this.logger.error(e); 98 } 99 100 /* tagName */ 101 try { 102 this.webdriver.findElement(By.tagName(locator)); 103 exist = true; 104 return exist; 105 } catch (NoSuchElementException e) { 106 this.logger.error(e); 107 } 108 109 return exist; 110 } 111 112 /** 113 * Verify the element exist or not(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) 114 * 115 * @author Aaron.ffp 116 * @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $ 117 * 118 * @param webdriver : WebDriver 119 * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) 120 * 121 * @return boolean 122 */ 123 public boolean isElementExist(WebDriver webdriver, String locator){ 124 boolean exist = false; 125 126 /* ID */ 127 try { 128 webdriver.findElement(By.id(locator)); 129 exist = true; 130 return exist; 131 } catch (NoSuchElementException e) { 132 this.logger.error(e); 133 } 134 135 /* name */ 136 try { 137 webdriver.findElement(By.name(locator)); 138 exist = true; 139 return exist; 140 } catch (NoSuchElementException e) { 141 this.logger.error(e); 142 } 143 144 /* cssSelector */ 145 try { 146 webdriver.findElement(By.cssSelector(locator)); 147 exist = true; 148 return exist; 149 } catch (NoSuchElementException e) { 150 this.logger.error(e); 151 } 152 153 /* xpath */ 154 try { 155 webdriver.findElement(By.xpath(locator)); 156 exist = true; 157 return exist; 158 } catch (NoSuchElementException e) { 159 this.logger.error(e); 160 } 161 162 /* linkText */ 163 try { 164 webdriver.findElement(By.linkText(locator)); 165 exist = true; 166 return exist; 167 } catch (NoSuchElementException e) { 168 this.logger.error(e); 169 } 170 171 /* className */ 172 try { 173 webdriver.findElement(By.className(locator)); 174 exist = true; 175 return exist; 176 } catch (NoSuchElementException e) { 177 this.logger.error(e); 178 } 179 180 /* partialLinkText */ 181 try { 182 webdriver.findElement(By.partialLinkText(locator)); 183 exist = true; 184 return exist; 185 } catch (NoSuchElementException e) { 186 this.logger.error(e); 187 } 188 189 /* tagName */ 190 try { 191 webdriver.findElement(By.tagName(locator)); 192 exist = true; 193 return exist; 194 } catch (NoSuchElementException e) { 195 this.logger.error(e); 196 } 197 198 return exist; 199 } 200 201 /** 202 * @function Verify the element exist or not 203 * 204 * @author Aaron.ffp 205 * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java isElementExistByXpath, 2014-11-23 4:03:52 Exp $ 206 * 207 * @param webdriver : WebDriver 208 * @param locator : XPath 209 * 210 * @return boolean 211 */ 212 public boolean isElementExistByXpath(WebDriver webdriver, String locator){ 213 boolean isExists = false; 214 215 try { 216 webdriver.findElement(By.xpath(locator)); 217 isExists = true; 218 } catch (NoSuchElementException nsee) { 219 this.logger.error(nsee); 220 nsee.printStackTrace(); 221 } 222 223 return isExists; 224 } 225 226 /** 227 * @function Verify the element exist or not 228 * 229 * @author Aaron.ffp 230 * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java isWebelementExistByXpath, 2014-11-23 4:03:52 Exp $ 231 * 232 * @param locator : XPath 233 * @return boolean 234 */ 235 public boolean isElementExistByXpath(String locator){ 236 boolean isExists = false; 237 238 try { 239 this.webdriver.findElement(By.xpath(locator)); 240 isExists = true; 241 } catch (NoSuchElementException nsee) { 242 this.logger.error(nsee); 243 nsee.printStackTrace(); 244 } 245 246 return isExists; 247 }
PS:当元素不可用或者隐藏式,返回的也是不存在,请知悉!
至此,WebUI 自动化功能测试脚本第 027-判断元素是否存在 顺利完结,希望此文能够给初学 Selenium 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
时间: 2024-11-08 23:46:38