Selenium:常见web UI元素操作及API使用
链接(link)
<div>
<p>链接 link</p>
<a href="www.cnblogs.com/tankxiao">小坦克</a>
</div>
链接的操作
// 找到链接元素
WebElement link1 = driver.findElement(By.linkText("小坦克"));
WebElement link11 = driver.findElement(By.partialLinkText("坦克")); // 点击链接
link1.click();
输入框 textbox
<div>
<p>输入框 testbox</p>
<input type="text" id="usernameid" value="username" />
</div>
输入框的操作
// 找到元素
WebElement element = driver.findElement(By.id("usernameid")); // 在输入框中输入内容
element.sendKeys("test111111"); // 清空输入框
element.clear(); // 获取输入框的内容
element.getAttribute("value");
按钮(Button)
<div>
<p>按钮 button</p>
<input type="button" value="添加" id="proAddItem_0" />
</div>
找到按钮元素
//找到按钮元素
String xpath="//input[@value='添加']";
WebElement addButton = driver.findElement(By.xpath(xpath));
// 点击按钮
addButton.click();
// 判断按钮是否enable
addButton.isEnabled();
下拉选择框(Select)
<div>
<p>下拉选择框框 Select</p>
<select id="proAddItem_kind" name="kind">
<option value="1">电脑硬件</option>
<option value="2">房产</option>
<option value="18">种类AA</option>
<option value="19">种类BB</option>
<option value="20">种类BB</option>
<option value="21">种类CC</option>
</select>
</div>
下拉选择框的操作
// 找到元素
Select select = new Select(driver.findElement(By.id("proAddItem_kind")));
// 选择对应的选择项, index 从0开始的
select.selectByIndex(2);
select.selectByValue("18");
select.selectByVisibleText("种类AA");
// 获取所有的选项
List<WebElement> options = select.getOptions();
for (WebElement webElement : options) {
System.out.println(webElement.getText());
}
单选按钮(Radio Button)
<div>
<p>单选项 Radio Button</p>
<input type="radio" value="Apple" name="fruit>" />Apple
<input type="radio" value="Pear" name="fruit>" />Pear
<input type="radio" value="Banana" name="fruit>" />Banana
<input type="radio" value="Orange" name="fruit>" />Orange
</div>
单选项元素的操作
// 找到单选框元素
String xpath="//input[@type='radio'][@value='Apple']";
WebElement apple = driver.findElement(By.xpath(xpath));
//选择某个单选框
apple.click();
//判断某个单选框是否已经被选择
boolean isAppleSelect = apple.isSelected();
// 获取元素属性
apple.getAttribute("value");
多选框 check box
<div>
<p>多选项 checkbox</p>
<input type="checkbox" value="Apple" name="fruit>" />Apple
<input type="checkbox" value="Pear" name="fruit>" />Pear
<input type="checkbox" value="Banana" name="fruit>" />Banana
<input type="checkbox" value="Orange" name="fruit>" />Orange
</div>
多选框的操作和单选框一模一样的。
Selenium:常见web UI元素操作及API使用的更多相关文章
- java selenium (九) 常见web UI 元素操作 及API使用
本篇介绍我们如何利用selenium 来操作各种页面元素 阅读目录 链接(link) <div> <p>链接 link</p> <a href=" ...
- 常见web UI 元素操作 及API使用
1. 链接(Link) // 找到链接元素,这个方法比较直接,即通过超文本链接上的文字信息来定位元素,这种方式一般专门用于定位页面上的超文本链接 WebElement link1 = driver.f ...
- python - web自动化测试 - 元素操作 - 窗口切换
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: 元素操作-切换.py @ide: PyCharm Community ...
- python - web自动化测试 - 元素操作 - 鼠标键盘
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: 鼠标操作.py @ide: PyCharm Community Edi ...
- python - web自动化测试 - 元素操作 - 等待
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: wait.py @ide: PyCharm Community Edi ...
- python - web自动化测试 - 元素操作 - 定位
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: find_ele.py @ide: PyCharm Community ...
- Python Selenium 搭建Web UI自动化
Python搭建UI自动化环境 下载Python3 Python官网 PyCharm 环境配置 安装Python 勾选Add Python to PATH,一直下一步. 验证:CMD输入Python ...
- robot framework 笔记(二),web ui 元素定位
背景: 元素定位的方式很多,有通过 id.name.class等等,有感兴趣的同学可以参看相关文档,本文介绍 跟过文字定位的方法,在工作中一直使用这一种 no bb 方法如下: 1.根据"我 ...
- Java Selenium - 元素操作 (二)
一篇概括了常用的元素定位方法,但是找到元素还是不够的,模拟鼠标的操作,完成各个功能点的自动操作才是关键. 下面是常见的页面元素操作会涉及到的方法,不是很全,比较复杂的后面单独拿出来做案例. 一, 输入 ...
随机推荐
- java学习路线-Java技术人员之路从0基础到高级
满满的 全是干货 java基础: 尚学堂 马士兵 个人推荐 历经5年锤练--史上最适合刚開始学习的人入门的Java基础视频 很具体 适合 时间多的看 传智播客java基础班 马士兵线程 ...
- SDWebImage源代码解析(一)
一.概念 SDWebImage是一个开源的第三方库,它提供了UIImageView的一个分类.以支持从远程server下载并缓存图片的功能. 二.优势 自从iOS5.0開始.NSURLCache也能够 ...
- Maven 命令行创建项目时 Could not find goal ‘create’ in plugin org.apache.maven.plugins:...
使用maven3.3.9 版本,进行命令行创建项目时输入以下命令创建失败 mvn archetype:create -DgroupId=com.zang.maven -DartifactId=sys ...
- js实现可兼容IE、FF、Chrome、Opera及Safari的音乐播放器
代码如下: /** 音乐播放器 * @param obj 播放器id * @param file 音频文件 mp3: ogg: * @param loop 是否循环 */ function audio ...
- Android自定义圆形进度条,完成类似LOFTER效果
1.http://stackoverflow.com/questions/3760381/rotating-image-animation-list-or-animated-rotate-androi ...
- MongoDB之索引
索引是用来加快查询的,这里不解说索引的原理和数据结构.事实上大部分数据库的索引就是B+Tree,想要了解的同学能够看索引原理,要掌握怎样为查询配置最佳索引会有些难度. MongoDB索引差点儿和关系型 ...
- Hive 作业优化
1.Join原则将条目少的表/子查询放在 Join的左边. 原因是在 Join 操作的 Reduce 阶段,位于 Join左边的表的内容会被加载进内存,将条目少的表放在左边,可以有效减少发生内存溢出的 ...
- Mysql 没有nvl()函数,却有一个类似功能的函数ifnull()
今天自己无聊写了看了一个查询需求随手写了一个sql语句,发现竟然不能运行,MySQL报[Err] 1305 - FUNCTION ceshi.nvl does not exist的错.才意识到自己写的 ...
- ffffff
http://www.ibm.com/developerworks/cn/linux/l-cn-linuxglb/ http://blog.csdn.net/wocjj/article/details ...
- 李洪强iOS开发之基于彻底解耦合的实验性iOS架构
基于彻底解耦合的实验性iOS架构 这周我决定做一个关于彻底解耦合的应用架构的实验.我想探究的主题是: “如果所有的应用内通讯都通过一个事件流来完成会怎么样?” 我构造了一个待办事项应用,因为这是我 ...