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 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...
随机推荐
- C#引用COM对象,报错:《类型 *** 未定义构造函数, 无法嵌入互操作类型 *** 。请改用适用的接口》的解决办法。
错误信息: 1.类型“SQLDMO.BackupClass”未定义构造函数 2.无法嵌入互操作类型“SQLDMO.BackupClass”.请改用适用的接口. 代码如下: ...
- CentOS 安装Gitolite 服务器
[jackluo@localhost .ssh]$ sudo groupadd git #创建 用户组 [jackluo@localhost .ssh]$ sudo adduser --system ...
- Java虚拟机(JVM)中的内存设置详解
在一些规模稍大的应用中,Java虚拟机(JVM)的内存设置尤为重要,想在项目中取得好的效率,GC(垃圾回收)的设置是第一步. PermGen space:全称是Permanent Generation ...
- JavaScript事件大全3
//无模式的提示框 //屏蔽按键 <html> <head> <meta http-equiv="Content-Type" content=& ...
- HTML&CSS布局练习---360导航页面
一共分为7个部分:由HTML和CSS外部样式表做成 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- Error 2147943712 during task creation
In a Windows 2008 server, when you confirm the creation of a new task, you may obtain the following ...
- Redis在windows环境下的部署
一.下载 官网地址:http://redis.io/download Git地址:https://github.com/MSOpenTech/redis 注:官方无windows版本,需要window ...
- empty()与remove([expr])的区别.转
jquery之empty()与remove()区别 要用到移除指定元素的时候,发现empty()与remove([expr])都可以用来实现.可仔细观察效果的话就可以发现.empty()是只移除了 ...
- FAQ&ubuntu12.04 gedit 打开 txt 文件乱码
ubuntu12.04 gedit 打开 windows 分区中的 txt 文件乱码,是因为 ubuntu 和 windows 两个系统的编码不同.解决办法:终端里依次输入以下2 条命令即可: 代码: ...
- 类型强转(type cast)
类型转换有 c 风格的,当然还有 c++风格的.c 风格的转换的格式很简单(TYPEEXPRESSION),但是 c 风格的类型转换有不少的缺点,有的时候用 c 风格的转换是不合适的, 因为它可以在任 ...