Selenium2学习-029-WebUI自动化实战实例-027-判断元素是否存在

时间:2022-11-03 01:39:43

非常简单的源码,敬请各位小主参阅。若有不足之处,敬请大神指正,不胜感激!

     /**
* Verify the element exist or not
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $
*
* @param by : By
*
* @return boolean
*/
public boolean isElementExist(By by){
boolean exist = false; try {
this.webdriver.findElement(by);
exist = true;
} catch (NoSuchElementException e) {
this.logger.error(e);
} return exist;
} /**
* Verify the element exist or not(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $
*
* @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @return boolean
*/
public boolean isElementExist(String locator){
boolean exist = false; /* ID */
try {
this.webdriver.findElement(By.id(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* name */
try {
this.webdriver.findElement(By.name(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* cssSelector */
try {
this.webdriver.findElement(By.cssSelector(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* Xpath */
try {
this.webdriver.findElement(By.xpath(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* linkText */
try {
this.webdriver.findElement(By.linkText(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* className */
try {
this.webdriver.findElement(By.className(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* partialLinkText */
try {
this.webdriver.findElement(By.partialLinkText(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* tagName */
try {
this.webdriver.findElement(By.tagName(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} return exist;
} /**
* Verify the element exist or not(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java isElementExist, 2015-1-22 3:13:34 Exp $
*
* @param webdriver : WebDriver
* @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @return boolean
*/
public boolean isElementExist(WebDriver webdriver, String locator){
boolean exist = false; /* ID */
try {
webdriver.findElement(By.id(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* name */
try {
webdriver.findElement(By.name(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* cssSelector */
try {
webdriver.findElement(By.cssSelector(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* xpath */
try {
webdriver.findElement(By.xpath(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* linkText */
try {
webdriver.findElement(By.linkText(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* className */
try {
webdriver.findElement(By.className(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* partialLinkText */
try {
webdriver.findElement(By.partialLinkText(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} /* tagName */
try {
webdriver.findElement(By.tagName(locator));
exist = true;
return exist;
} catch (NoSuchElementException e) {
this.logger.error(e);
} return exist;
} /**
* @function Verify the element exist or not
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java isElementExistByXpath, 2014-11-23 4:03:52 Exp $
*
* @param webdriver : WebDriver
* @param locator : XPath
*
* @return boolean
*/
public boolean isElementExistByXpath(WebDriver webdriver, String locator){
boolean isExists = false; try {
webdriver.findElement(By.xpath(locator));
isExists = true;
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return isExists;
} /**
* @function Verify the element exist or not
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java isWebelementExistByXpath, 2014-11-23 4:03:52 Exp $
*
* @param locator : XPath
* @return boolean
*/
public boolean isElementExistByXpath(String locator){
boolean isExists = false; try {
this.webdriver.findElement(By.xpath(locator));
isExists = true;
} catch (NoSuchElementException nsee) {
this.logger.error(nsee);
nsee.printStackTrace();
} return isExists;
}

PS:当元素不可用或者隐藏式,返回的也是不存在,请知悉!

至此,WebUI 自动化功能测试脚本第 027-判断元素是否存在 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

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