Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素
非常简单的方法封装,就不啰嗦了,直接上码咯 ^_^
/**
* 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 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素的更多相关文章
- Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值
在自动化脚本编写过程中,经常需要获取页面元素的文本进行判断,以便对于不同的文本进行不同的处理.比如:很多的购物网站,加入购物车的按钮是有多个状态的(加入购物车.到货通知.暂不销售等),那么在实际的操作 ...
- Selenium2学习-009-WebUI自动化实战实例-007-Selenium 8种元素定位实战实例源代码(百度首页搜索录入框及登录链接)
此 文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,通过 ID.name.xpath.cssSelector.linkText.className.partialLinkTe ...
- Selenium2学习-034-WebUI自动化实战实例-032-获取页面 body 大小
获取 body 元素大小的方法,非常简单,直接上码,敬请参阅! /** * Get body size * * @author Aaron.ffp * @version V1.0.0: autoSel ...
- Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置
此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...
- Selenium2学习-035-WebUI自动化实战实例-033-页面快照截图应用之三 -- 区域截图(专业版)
之前有写过两篇博文讲述了 WebUI 自动化测试脚本中常用的截图方法,敬请参阅如下所示链接: 浏览器显示区域截图 浏览器指定区域截图 那么当需要截取的区域不在浏览器显示窗口范围之内时,之前的方法显然无 ...
- Selenium2学习-007-WebUI自动化实战实例-005-解决 Firefox 版本不兼容:org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary
此文主要讲述 Java 运行 Selenium 脚本时,因 Friefox 浏览器版本与 selenium-server-standalone-x.xx.x.jar 不兼容引起的 org.openqa ...
- Selenium2学习-027-WebUI自动化实战实例-025-JavaScript 在 Selenium 自动化中的应用实例之三(页面滚屏,模拟鼠标拖动滚动条)
日常的 Web UI 自动化测试过程中,get 或 navigate 到指定的页面后,若想截图的元素或者指定区域范围不在浏览器的显示区域内,则通过截屏则无法获取相应的信息,反而浪费了无畏的图片服务器资 ...
- Selenium2学习-039-WebUI自动化实战实例-文件上传下载
通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...
- Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题
日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...
随机推荐
- How to Programmatically Switch between the HubTile Visual States
In this post I am going to talk about how to programmatically switch between different HubTile Visua ...
- 【wikioi】1018 单词接龙
题目链接 算法:DFS+考你阅题 题目描述: 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中 ...
- org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot create directory /user/hive/warehouse/page_view. Name node is in safe mode
FAILED: Error in metadata: MetaException(message:Got exception: org.apache.hadoop.ipc.RemoteExceptio ...
- placeholder在不同浏览器下的表现及兼容方法
1.什么是placeholder? placeholder是html5新增的一个属性,当input或者textarea设置了该属性后,该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点(或 ...
- PCL 1.6.0 VS2010 Configuration
Open VS2010, create a new project, then open Property Manager, double-click Microsoft.Cpp.win32.user ...
- 用AE如何制作如下三个loading动效,
在本期象牙绘UED团队分享当中,我们将详细演示用AE如何制作如下三个loading动效, 其中涉及到AE表达式的应用.值曲线调整.速度曲线编辑等知识. 对于初学者来说可能信息量略大,希望通过是视频教程 ...
- Html - 横版TH+TD
通常可位于表格上方的搜索区域html <div class="panel panel-default"> <div class="panel-body& ...
- C# - Lee 公共类库
我的公共类库 using System; using System.IO; using System.Net; using System.Security.Cryptography; using Sy ...
- NOJ 1072 The longest same color grid(尺取)
Problem 1072: The longest same color grid Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit in ...
- 【转】const和static readonly
我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声 ...