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 - 元素操作 (二)
一篇概括了常用的元素定位方法,但是找到元素还是不够的,模拟鼠标的操作,完成各个功能点的自动操作才是关键. 下面是常见的页面元素操作会涉及到的方法,不是很全,比较复杂的后面单独拿出来做案例. 一, 输入 ...
随机推荐
- 每日一个机器学习算法——adaboost
在网上找到一篇好文,直接粘贴过来,加上一些补充和自己的理解,算作此文. My education in the fundamentals of machine learning has mainly ...
- unity, use particleSystem with UGUI
要想在UGUI上添加particleSystem,需要将Canvas的Render Mode设置为Screen Space - Camera,并为其Render Camera指定一个Orthograp ...
- vivado2016.2下系统自带DDR3 ip例程仿真运行
背景:从ISE14.7迁移到vivado2016.2. xilinx的软件改的真是不一般的大.两个软件操作差距真是让人想骂人.由于项目需要,准备调试DDR3.对于新手来说,例化一个DDR3 ip.如果 ...
- 浅谈Java中的System.gc()的工作原理
很多人把Java的“效率低下”归咎于不能自由管理内存,但我们也知道将内存管理封装起来的好处,这里就不赘述. Java中的内存分配是随着new一个新的对象来实现的,这个很简单,而且也还是有一些可以“改进 ...
- C++语言基础(5)-this和static关键字
一.this关键字 this是一个指针,可用其访问成员变量或成员函数 下面是使用this的一个完整示例: #include <iostream> using namespace std; ...
- C语言基础(13)-函数
一. 函数的原型和调用 在使用函数前必须定义或者声明函数. double circle(double r); int main() { ); printf("length = %f\n&qu ...
- ARM处理器的运行模式
ARM处理器的7种运行模式 用户模式( usr ):ARM处理器正常的程序执行状态: 快速中断模式( fiq ):用于高速数据传输或通道处理: 外部中断模式( irq):用于通常的中断处理: 管理模式 ...
- 用@spy模拟真实对象的部分行为
1.说明在某些情况下,我们需要使用一个真实对象.但是,我们同时需要自定义该对象的部分行 为,此时用@spy 就可以帮我们达到这个目的. 2.用法: categoryService = PowerMoc ...
- android 阿拉伯语下,图库中编辑运动轨迹图片,动画中会显示绿色的图片
alps/packages/apps/Camera/src/com/android/camera/FileSaver.java 1:import java.util.Locale; 2:modify ...
- 机器人的运动范围 剑指offer66题
include "stdafx.h" #include<vector> #include<algorithm> #include<string> ...