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()方法进行执行 ...
随机推荐
- 传感器之超声波测距HC-SR04
一.前言 HC-SR04超声波测距模块可提供2cm-400cm的非接触式距离感测功能,测距精度可达高到3mm:模块包括超声波发射器.接收器与控制电路.像智能小车的测距以及转向,或是一些项目中,常常会用 ...
- stm32 cubemx hal 工程中 微秒延迟 delay_us
参考的正点原子的代码 测试平台 stm32f429i-disco 配了一个gpio 时钟 gpio /* USER CODE BEGIN 0 */ typedef uint8_t u8; typede ...
- libevent源码分析:listener
listener是libevent封装的一个方便生成监听者的一组结构和函数,其中包括: /* * Copyright (c) 2000-2007 Niels Provos <provos@cit ...
- libevent源码分析:time-test例子
time-test例子是libevent自带的一个例子,通过libevent提供的定时事件来实现,间隔固定时间打印的功能. /* * gcc -g -o time-test time-test.c - ...
- Git使用指南(1)——Git配置命令
配置用户信息 git config --global user.name bongxin git config --global user.email bongxin@yeah.net 配置文本编辑器 ...
- Python mysql sqlite 数据没有更新
原因 MySQL事务隔离级别 解决方案 conn.commit() conn.close()
- google浏览器打开报出文件不可读解决方案
1.打开*.desktop文件 gedit ~/.local/share/applications/name.desktop 在文件中做改动: The .desktop file should loo ...
- Xcode 8
(一)Xcode8去掉多余LOG 1.打开我们的项目,进入EditScheme 2.我们在Environment Variables中添加OS_ACTIVITY_MODE=disable
- 使用idea15搭建基于maven的springmvc-mybatis框架
我这边使用的是intellij idea15 1.new maven webapp project 2.添加groupId和artifactId 3.选择maven路径和maven仓库路径 最后确定之 ...
- Word,PDF,PPT,TXT之间的转换方法
来源: 刘波的日志 一.把PPT转WORD形式的方法 1.利用"大纲"视图 打开PPT演示文稿,单击"大纲",在左侧"幻灯片/大纲”任务窗格的“大纲” ...