Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素
2015-08-02 12:09
585 查看
非常简单的方法封装,就不啰嗦了,直接上码咯 ^_^
至此,WebUI 自动化功能测试脚本第 024-获取页面元素 顺利完结,希望此文能够给初学 Selenium 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
/** * 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 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
相关文章推荐
- uuid (guid)全局唯一标识符
- Android SDK Tools Platform-tools Build-tools
- hdu 5329 Question for the Leader 枚举,划分图为k个联通的子图
- Gym 100570E : Palindrome Query
- IOS开发之UIScrollViewDelegate详解
- iOS 人机交互设计(开发)指南及一些综合知识整合
- Codeforces 558 D. Guess Your Way Out! II
- Selenium2学习-025-WebUI自动化实战实例-023-页面快照截图应用之一 -- 常规截图(全页面)
- 1099. Build A Binary Search Tree (30)
- Web富文本编辑器---UEditor
- easyUI 验证控件应用、自定义、扩展验证 手机号码或电话话码格式
- Android Api Demos登顶之路(十四)Quick Contacts
- 猫猫学iOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现
- HADOOP cluster some issue for installation
- RequireJS模块化加载
- 周赛-Clique in the Divisibility Graph
- 周赛-Clique in the Divisibility Graph 分类: 比赛 2015-08-02 09:02 23人阅读 评论(3) 收藏
- CodeForces 484B 数学 Maximum Value
- Integer Inquiry
- 使用rpmbuild来创建自己的RPM