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 - 元素操作 (二)
一篇概括了常用的元素定位方法,但是找到元素还是不够的,模拟鼠标的操作,完成各个功能点的自动操作才是关键. 下面是常见的页面元素操作会涉及到的方法,不是很全,比较复杂的后面单独拿出来做案例. 一, 输入 ...
随机推荐
- __block 和__weak 区别及使用
API Reference对__block变量修饰符有如下几处解释: //A powerful feature of blocks is that they can modify variables ...
- jQuery中事件绑定
一.前言 最近在做项目中要用到jQuery来绑定事件,首先想到的是$(selector).事件名();这样绑定事件的方式,这种方式对事件进行绑定其实也就是bind()方法,但当选择器匹配的元素过多,$ ...
- 用VMware 8安装Ubuntu 12.04具体过程(图解)
图解演示环境版本号: 本机系统: WIN7 虚拟机:VMware Workstation 8 (英文版) 安装目标:Ubuntu Desktop 12.04 LTS (请点击这里)先下载好iso镜像 ...
- centos7下安装mysql5.7和jdk 1.8
安装mysql5.7 具体安装过程可参见官网:A Quick Guide to Using the MySQL Yum Repository 进入/usr/local/src文件夹. cd /usr/ ...
- 从零开始学习SSH框架笔记之四 几个关键配置文件备忘(模板)
不多说,直接上代码.关于注释我尽量写详细点. 1.web.xml <?xml version="1.0" encoding="UTF-8"?> &l ...
- python统计订单走势
#coding=utf-8 import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotl ...
- Solve Equation gcd(x,y)=gcd(x+y,lcm(x,y)) gcd(x,y)=1 => gcd(x*y,x+y)=1
/** 题目:Solve Equation 链接:http://acm.hnust.edu.cn/JudgeOnline/problem.php?id=1643 //最终来源neu oj 2014新生 ...
- PHP安装加载yaf扩展
Yaf,全称 Yet Another Framework,是一个C语言编写的PHP框架,是一个用PHP扩展形式提供的PHP开发框架, 相比于一般的PHP框架, 它更快. 它提供了Bootstrap, ...
- openfile
linux修改open files数 概要 linux系统默认open files数目为1024, 有时应用程序会报Too many open files的错误,是因为open files 数目不 ...
- Spring MVC控制器类名称处理映射
以下示例显示如何使用Spring Web MVC框架使用控制器类名称处理程序映射. ControllerClassNameHandlerMapping类是基于约定的处理程序映射类,它将URL请求映射到 ...