Selenium2.0功能测试之Web元素的定位

简介:
页面元素的定位可以说是WebDriver中最核心的内容了,我们定位元素的目的主要有:操作元素,获取该元素的属性,获取元素的text以及获取元素的数量,WebDriver 为我们提供了以下几种方法来帮我们定位 web元素:
  通过元素的id获取
  通过元素的name获取
  通过元素的tag name 获取
  通过css xpath 获取
  通过xpath 获取
  通过class name  获取
  通过一部分的link text 获取元素
  通过全部的link text 获取元素
   唯一元素的定位:
package org.coderinfo.demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FindSingleElements {
private static final String URL = "file:///C:/Desktop/Selenium/login.html"; // 需要更改这个URL到你自己的login.html 的文件路径
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize(); //最大化浏览器界面
driver.get(URL); //访问谷哥的首页 ,此处放弃度娘。
Thread.sleep(2000); //Wait for page load
driver.findElement(By.id("inputEmail")).sendKeys("coderinfo@163.com"); // use id to find a web element
Thread.sleep(2000);
driver.findElement(By.name("password")).sendKeys("#####");    // use name to find a web element
Thread.sleep(2000);
driver.findElement(By.cssSelector("#inputEmail")).clear();   // use css selector to find a web element
Thread.sleep(2000);
driver.findElement(By.linkText("UseLink")).click();  // use link text to find a web element
Thread.sleep(2000);
driver.findElement(By.partialLinkText("Use")).click(); // use partial link text to find a web element
Thread.sleep(2000);
String formClassName = driver.findElement(By.tagName("form")).getAttribute("class");  //use tag name to find a web element
System.out.println(formClassName);
Thread.sleep(2000);
String text = driver.findElement(By.xpath("/html/body/form/div[1]/div")).getText();  // use xpath to find a web element
System.out.println(text);
String inputText = driver.findElement(By.className("inputClass")).getAttribute("placeholder");  // use class name to find a web element
System.out.println(inputText);
Thread.sleep(5000);
driver.quit();  //彻底退出WebDriver
}
}

字体:        | 上一篇 下一篇 | 打印  | 我要投稿 

   这里是要测试的页面login.html的源码:
<!DOCTYPE html>
<html>
<head>
<title>For Selenium Test</title>
<style type="text/css">
div {
margin-top:10px
}
#inputEmail {
color:red
}
</style>
</head>
<body>
<center>
<h3>Find Single Element</h3>
</center>
<form class="form-h">
<div class="items">
<div class="item">
Use ID:<input type="text" id="inputEmail" name="email" placeholder="Email"/>
</div>
</div>
<div class="items">
<div class="item">
Use Name:<input type="password" id="inputPassword" name="password" placeholder="Password" class="inputClass"/>
</div>
</div>
<div class="items">
<div class="item">
Use Link:<a href="#">UseLink</a>
</div>
</div>
</form>
</body>
</html>
   一组元素的定位 :
package org.coderinfo.demo;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FindElements {
private static final String URL = "file:///C:/user/Desktop/Selenium/checkbox.html";  //改为你自己的url
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();  //create a chrome driver
driver.manage().window().maximize();  // max size the chrome window
driver.get(URL);   //open URL with the chrome browser
try {
Thread.sleep(2000);                  // wait for web loading
} catch (InterruptedException e) {
e.printStackTrace();
}
List<WebElement> webElements = driver.findElements(By.cssSelector("input[type='checkbox']"));  // Use css selector to get all the checkbox
for (WebElement webElement : webElements) {   // loop through all elements
webElement.click();    // click current element == select the current checkbox
}
System.out.println("Count: " + webElements.size());  //print the count of all the elements
try {
Thread.sleep(3000);  // wait 3s
} catch (InterruptedException e) {
e.printStackTrace();
}
webElements = driver.findElements(By.tagName("input"));  // use tag name to get all the checkbox
webElements.get(webElements.size()-1).click();  // Cancel the last selected checkbox
try {
Thread.sleep(5000);  // wait 5s
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.quit();  // close webdriver
}
}

   测试页面checkbox.html的代码:
<!DOCTYPE html>
<html>
<head>
<title>Get ALl CheckBox</title>
<style type="text/css">
h2 {
text-align:center
}
</style>
</head>
<body>
<h2>CheckBox<h2/>
<form class="form-h">
<div class="input-c">
<input type="checkbox" class="in" id="in1"/>
<div>
<div class="input-c">
<input type="checkbox" class="in" id="in2"/>
<div>
<div class="input-c">
<input type="checkbox" class="in" id="in3"/>
<div>
<div class="input-c">
<input type="checkbox" class="in" id="in4"/>
<div>
<div class="input-c">
<input type="checkbox" class="in" id="in5"/>
<div>
</form>
</body>
</html>
相关文章:
   


最新内容请见作者的GitHub页:http://qaseven.github.io/
  
目录
相关文章
|
5天前
|
Java 测试技术 Python
《手把手教你》系列技巧篇(三十六)-java+ selenium自动化测试-单选和多选按钮操作-番外篇(详解教程)
【4月更文挑战第28天】本文简要介绍了自动化测试的实战应用,通过一个在线问卷调查(&lt;https://www.sojump.com/m/2792226.aspx/&gt;)为例,展示了如何遍历并点击问卷中的选项。测试思路包括找到单选和多选按钮的共性以定位元素,然后使用for循环进行点击操作。代码设计方面,提供了Java+Selenium的示例代码,通过WebDriver实现自动答题。运行代码后,可以看到控制台输出和浏览器的相应动作。文章最后做了简单的小结,强调了本次实践是对之前单选多选操作的巩固。
14 0
|
2天前
|
JavaScript 前端开发 UED
【Web 前端】如何将一个 HTML 元素添加到 DOM 树中的?
【5月更文挑战第2天】【Web 前端】如何将一个 HTML 元素添加到 DOM 树中的?
|
3天前
|
XML 前端开发 Oracle
16:JSP简介、注释与Scriptlet、Page指令元素、Include操作、内置对象、四种属性-Java Web
16:JSP简介、注释与Scriptlet、Page指令元素、Include操作、内置对象、四种属性-Java Web
9 2
|
3天前
|
JavaScript 前端开发
【Web 前端】网页上有 5 个div元素,如何使用JQ来选择它们?
【5月更文挑战第1天】【Web 前端】网页上有 5 个div元素,如何使用JQ来选择它们?
|
4天前
|
敏捷开发 监控 测试技术
探索自动化测试工具Selenium Grid的高效集成策略
【4月更文挑战第30天】在现代Web应用的快速迭代和持续部署中,测试自动化已成为确保产品质量的关键。Selenium Grid作为一款支持多种浏览器和操作系统的测试工具,提供了并行执行测试用例的能力,极大地提升了测试效率。本文将深入探讨如何高效地将Selenium Grid集成到现有的测试框架中,以及实施过程中的最佳实践,帮助团队最大化测试覆盖率,同时降低资源消耗。
|
4天前
|
数据管理 测试技术
深入理解自动化测试框架:以Selenium为例
【4月更文挑战第30天】 随着软件开发的快速发展,自动化测试已经成为保证软件质量和提升开发效率的重要手段。本文将深入探讨自动化测试框架的核心概念,并以广泛应用的开源工具Selenium为例,解析其架构、原理及在实际项目中的运用。通过实例分析与性能评估,旨在为读者提供一套系统的自动化测试解决方案,并探讨其在复杂应用场景下的优化策略。
|
4天前
|
移动开发 JavaScript 前端开发
【专栏:HTML进阶篇】HTML模板与Web组件:可复用的网页元素
【4月更文挑战第30天】HTML模板和Web组件提升网页开发效率和可维护性。HTML模板,如&lt;template&gt;元素和服务器端模板引擎,用于创建可复用的HTML结构。Web组件是自定义的HTML元素,结合影子DOM和模板,实现封装的可重用组件。两者助力构建高效、现代的网页和网站。
|
5天前
|
敏捷开发 前端开发 JavaScript
深入理解自动化测试框架:以Selenium为例
【4月更文挑战第30天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加快市场投放的关键步骤。本文聚焦于流行的自动化测试框架——Selenium,探讨其架构、核心组件以及如何有效地利用Selenium进行Web应用测试。通过分析真实案例,我们将揭示Selenium在实际项目中的应用优势与面临的挑战,并提出优化策略。文章的目的在于帮助测试工程师深入理解Selenium,提升其在复杂项目中的运用效率。
|
5天前
|
前端开发 IDE 数据可视化
深入理解与应用自动化测试框架Selenium的最佳实践
【4月更文挑战第30天】 本文将深入剖析自动化测试框架Selenium的核心原理,并结合最佳实践案例,探讨如何有效提升测试覆盖率和效率。文中不仅涉及Selenium的架构解析,还将提供针对性的策略来优化测试脚本,确保测试流程的稳定性与可靠性。通过实例演示,读者可以掌握如何在不同测试场景中灵活运用Selenium,以及如何处理常见的技术挑战。
|
5天前
|
中间件 测试技术 API
探索自动化测试工具的新边界:Selenium与Appium的集成实践
【4月更文挑战第30天】 随着移动应用和Web应用的不断融合,传统的自动化测试工具需要适应新的测试环境。本文将详细分析Selenium和Appium这两款流行的自动化测试工具的集成实践,探讨如何构建一个能够同时支持Web和移动端应用的自动化测试框架。通过对比两者的技术架构、功能特性以及在实际项目中的集成过程,我们旨在为读者提供一个清晰的指导,帮助他们在复杂的应用环境中实现高效、稳定的自动化测试流程。