您的位置:首页 > 产品设计 > UI/UE

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

2015-08-02 12:09 585 查看
非常简单的方法封装,就不啰嗦了,直接上码咯 ^_^

/**
* Get element. It will be return null when there is not such element.
*
* @author Aaron.ffp
* @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getWebElement, 2015-7-31 13:56:59 Exp $
*
* @param by : By
*
* @return WebElement
*/
public WebElement getElement(By by){
try {
return this.webdriver.findElement(by);
} catch (Exception e) {
return null;
}
}

/**
* Get element by locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java getElement, 2015-1-22 3:15:57 Exp $
*
* @param locator  : the expression of locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @return WebElement
*/
public WebElement getElement(String locator){
WebElement webelement = null;

/* by ID */
try {
return this.webdriver.findElement(By.id(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by name */
try {
return this.webdriver.findElement(By.name(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by xpath */
try {
return this.webdriver.findElement(By.xpath(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by cssSelector */
try {
return this.webdriver.findElement(By.cssSelector(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by linkText */
try {
return this.webdriver.findElement(By.linkText(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by className */
try {
return this.webdriver.findElement(By.className(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by partialLinkText */
try {
return this.webdriver.findElement(By.partialLinkText(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by tagName */
try {
return this.webdriver.findElement(By.tagName(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

return webelement;
}

/**
* Get element by locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.sele.demo IsWebelementExist.java getElement, 2015-1-22 3:15:57 Exp $
*
* @param webdriver      : WebDriver
* @param locator        : the expression of locator(ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName)
*
* @return WebElement
*/
public WebElement getElement(WebDriver webdriver, String locator){
WebElement webelement = null;

/* by ID */
try {
return webdriver.findElement(By.id(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by name */
try {
return webdriver.findElement(By.name(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by xpath */
try {
return webdriver.findElement(By.xpath(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by cssSelector */
try {
return webdriver.findElement(By.cssSelector(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by linkText */
try {
return webdriver.findElement(By.linkText(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by className */
try {
return webdriver.findElement(By.className(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by partialLinkText */
try {
return webdriver.findElement(By.partialLinkText(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

/* by tagName */
try {
return webdriver.findElement(By.tagName(locator));
} catch (NoSuchElementException e) {
this.logger.error(e);
webelement = null;
}

return webelement;
}


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

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: