java selenium (九) 常见web UI 元素操作 及API使用
本篇介绍我们如何利用selenium 来操作各种页面元素
链接(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>
多选框的操作和单选框一模一样的, 这里就不再讲了
java selenium (九) 常见web UI 元素操作 及API使用的更多相关文章
- Selenium:常见web UI元素操作及API使用
链接(link) <div> <p>链接 link</p> <a href="www.cnblogs.com/tankxiao">小 ...
- 常见web UI 元素操作 及API使用
1. 链接(Link) // 找到链接元素,这个方法比较直接,即通过超文本链接上的文字信息来定位元素,这种方式一般专门用于定位页面上的超文本链接 WebElement link1 = driver.f ...
- Java&Selenium调用JS实现高亮被操作页面元素高亮
Java&Selenium调用JS实现高亮被操作页面元素高亮 /* * the method of invoking js to do something * * @author daviey ...
- 《手把手教你》系列技巧篇(七十一)-java+ selenium自动化测试-自定义类解决元素同步问题(详解教程)
1.简介 前面宏哥介绍了几种关于时间等待的方法,也提到了,在实际自动化测试脚本开发过程,百分之90的报错是和元素因为时间不同步而发生报错.本文介绍如何新建一个自定义的类库来解决这个元素同步问题.这样, ...
- python - web自动化测试 - 元素操作 - 窗口切换
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: 元素操作-切换.py @ide: PyCharm Community ...
- Selenium系列(十九) - Web UI 自动化基础实战(6)
如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...
- 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 ...
- Java+selenium之WebDriver模拟鼠标键盘操作(六)
org.openqa.selenium.interactions.Actions类,主要定义了一些模拟用户的鼠标mouse,键盘keyboard操作.对于这些操作,使用 perform()方法进行执行 ...
随机推荐
- C++中 OOP相关的类型转换
我们都知道,在C++中有很多类型转换.今天在这里,我们不讨论普通变量的类型转换(比如int转换成double等等).本文主要讨论面向对象相关的类型转换:向上转换和向下转换. 首先,我们定义一个基类Ba ...
- 构建ceph deb 安装包
前言:本文基于ubuntu 14.04.5 LTS 和ceph v0.94.3 之上做的实验 一.编译ceph包1.1.克隆ceph代码,切换分支 git clone --recursive http ...
- 在Excel中制作金字塔条形图
使用场景:一项市场调查研究中,男性和女性.赞同和反对.满意和不满意的两方面的消费者,他们在某些项目上的指标分布特性一项产品组合决策中,乐观场景和悲观场景下各产品的获利情况一个产品试销活动中,不同门店渠 ...
- ubuntu 14.04 vsftpd安装问题
sudo apt-get install vsftpd; 打开允许访问用户名 local_enable=YES write_enable=YES chroot_list_file=/etc/vsftp ...
- sublimetext3备份
http://files.cnblogs.com/files/hwd13/Data.zip http://files.cnblogs.com/files/hwd13/sublime3.zip
- Log4net(1):配置的简单说明
基础代码下载地址:https://github.com/zhangsai521314/StudyLog4net 1:按日期分隔日志 <configSections> <section ...
- iOS 消息推送报错NSCocoaErrorDomain Code=3000
转自: http://www.cnblogs.com/zxykit/p/5207498.html Xcode7推送报错.Error Domain=NSCocoaErrorDomain Code=300 ...
- viewPager 的可滑动 Title
有三种方式: 1. 系统提供的title 缺点:标题在viewpager的滑动过程中也会滑动 实现:在ViewPager布局中添加 PagerTabStrip 或者是 PagerTitleStrip ...
- join / left join / right join
1 可以自己join自己 SELECT * FROM table_1 t1join table_1 ton t.target_id = t1.target_id 2 join要双方都满足才可以出现结果 ...
- linux centos 安装mysql
安装步骤 http://www.cnblogs.com/gaojupeng/p/5727069.html 下面这个报错 主要还是在 题啊加软连接的 命令出了问题 1.启动 报错 mysqld_s ...