java selenium 常见web UI 元素操作及API使用

作者:肖佳 时间:2021-08-30 00:50:18 

本篇介绍我们如何利用selenium 来操作各种页面元素

阅读目录

  1. 链接(link)

  2. 输入框 textbox

  3. 按钮(Button)

  4. 下拉选择框(Select)

  5. 单选按钮(Radio Button)

  6. 多选框 check box

链接(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,webUI
0
投稿

猜你喜欢

  • 一文带你学会规则引擎Drools的应用

    2022-04-03 08:40:59
  • C#禁用双击窗体图标关闭窗体的方法

    2022-01-04 14:20:04
  • Android开发手册TextInputLayout样式使用示例

    2023-10-16 11:03:12
  • Java基础之static关键字的使用讲解

    2023-10-06 01:26:25
  • Java文件上传下载、邮件收发实例代码

    2022-07-05 06:40:00
  • Java二维数组简单定义与使用方法示例

    2022-01-28 08:31:37
  • 使用Hibernate根据实体类自动生成表的方法

    2022-12-20 16:54:44
  • C#调用SQLite的方法实例分析

    2022-09-25 06:02:22
  • android开发之蜂鸣提示音和震动提示的实现原理与参考代码

    2022-11-22 21:47:38
  • SpringBoot加密配置文件的SQL账号密码方式

    2023-08-23 08:59:42
  • C#泛型约束的深入理解

    2023-02-21 09:32:19
  • struts2实现多文件上传

    2023-11-23 02:27:53
  • Android自定义view贝塞尔曲线

    2023-04-09 07:51:22
  • 教你5分钟轻松搞定内存字节对齐

    2022-05-03 08:35:10
  • C#实现递归调用的Lambda表达式

    2022-10-03 05:13:29
  • SpringBoot控制配置类加载顺序方式

    2022-08-18 04:31:15
  • 基于C#方法重载的总结详解

    2022-07-29 13:19:46
  • Spring Boot获取微信用户信息的超简单方法

    2023-04-19 19:47:23
  • springboot如何将http转https

    2023-02-27 17:10:45
  • MVC设定默认路由为指定的Area下的某个action

    2023-03-03 00:00:56
  • asp之家 软件编程 m.aspxhome.com