Selenium-基础操作
一、测试代码
@Test
public void test() {
WebDriver driver = new FirefoxDriver();
// 打开当前包中的index页面
driver.get("file:///D:/%E8%B5%B5%E6%AC%A2/Selenium/Selenium/src/com/html/index.html");
WaitSeconds(1000);
// 清除用户输入
driver.findElement(By.id("fname")).clear();
WaitSeconds(1000);
// 输入
driver.findElement(By.id("fname")).sendKeys("这是输入的内容");
WaitSeconds(1000);
// 获取元素内容
String text = driver.findElement(By.name("jsh")).getText();// 这种方法是获取元素的文本值
System.out.println("获取元素内容" + text);
WaitSeconds(1000);
// 以下这种方式是获取input的value值
text = driver.findElement(By.id("fname")).getAttribute("value");
System.out.println("value获取元素内容" + text);
WaitSeconds(1000);
// 单击操作
driver.findElement(By.id("idOfButton")).click();
WaitSeconds(1000);
// 通过浏览器记录向后导航
driver.findElement(By.linkText("This is a link")).click();
driver.navigate().back();
WaitSeconds(1000);
// 向前导航
driver.navigate().forward();
// 刷新
driver.navigate().refresh();
// 关闭网页当前页面
driver.close();
// 关闭浏览器,如果有其他选项卡一并关闭
driver.quit();
// 在windows间移动
WaitSeconds(3000);
driver.switchTo().window("MsgWindow");
// 拖拽,首先拖拽的对象要实现拖拽的方法
WaitSeconds(3000);
WebElement source=driver.findElement(By.id("sourceImage"));
WebElement target=driver.findElement(By.id("targetDiv"));
Actions actions=new Actions(driver);
actions.dragAndDrop(source, target).build().perform();
//actions.clickAndHold(source).moveToElement(target).perform();
// 通过标签
driver.findElement(By.tagName("input")).sendKeys("aaaaa");
// 通过linktext
driver.findElement(By.linkText("This is a link")).click();
driver.findElement(By.partialLinkText("This is")).click();
// 处理下拉列表,找到元素之后new一个select对象
WebElement dElement=driver.findElement(By.id("testingDropdown"));
Select dropdownlist=new Select(dElement);
dropdownlist.selectByIndex(3);
WaitSeconds(2000);
dropdownlist.selectByValue("Performance");
WaitSeconds(2000);
dropdownlist.selectByVisibleText("Manual Testing");
WaitSeconds(2000);
dropdownlist.deselectByIndex(2);
// Alert
driver.findElement(By.id("Alert")).click();
WaitSeconds(2000);
driver.switchTo().alert().accept();
WaitSeconds(2000);
driver.findElement(By.id("Confirm")).click();
WaitSeconds(2000);
String txt=driver.switchTo().alert().getText();
System.out.println(txt);
WaitSeconds(2000);
driver.switchTo().alert().dismiss();
// 滚动,需要调用js方法scrollBy(x,y)
JavascriptExecutor javascriptExecutor=(JavascriptExecutor)driver;
javascriptExecutor.executeScript("scrollBy(0,4000)");
}
二、以上测试代码针对的html页面
<html>
<head>
<title>简单测试页面</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="src/com/html/bootstrap.min.css">
<script src="src/com/html/jquery-3.3.1.min.js"></script>
<script src="src/com/html/bootstrap.min.js"></script>
<style></style>
</head>
<body style="font-family: cursive;">
<div class="container">
<div class="row">
<div class="col-md-offset-2 col-md-8" style="font-size: 30; margin-top: 40px; ">
用于自动化测试的Web页面示例
</div>
</div>
<div class="row">
<div class="col-md-12" style="font-size:20px; margin-top:40px;" name="jsh">
This is sample webpage with dummy elements that will help you in learning selenium automation.
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<b>This is sample text.</b>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p> <b>Link : </b><a href="https://www.yiibai.com/">This is a link</a></p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p><b>TextBox : </b><input id="fname" type="text" name="firstName" value="文本框内容" ><input id="fname2" type="text" name="firstName" value="第二个文本框" ></p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p><b>Button : </b><button id="idOfButton" title="Click me!!" type="button" onclick="this.style.background='green';">Submit</button></p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p><b>Radio button : </b>
<form action="#">
<input id="male" type="radio" name="gender" value="male"> Male
<input id="female" type="radio" name="gender" value="female"> Female
</form>
</p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p><b>Checkbox :</b>
<form action="#">
<input type="checkbox" class="Automation" value="Automation"> Automation Testing
<input type="checkbox" class="Performance" value="Performance"> Performance Testing
</form>
</p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p><b>Drop down :</b>
<select id="testingDropdown">
<option id="automation" value="Automation">Automation Testing</option>
<option id="performance" value="Performance">Performance Testing</option>
<option id="manual" value="Manual">Manual Testing</option>
<option id="database" value="Database">Database Testing</option>
</select>
</p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p><button id="dblClkBtn" ondblclick="alert('hi, Yiibai Testing');">Double-click to generate alert box</button></p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p><b>Click button to generate Alert box : </b>
<button id="Alert" onclick="alert('hi, Yiibai Testing');">Generate Alert Box</button>
</p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p> <b> Click button to generate Confirm box : </b>
<button id="confirm" onclick="generateConfirmBox()">Generate Confirm Box</button>
</p>
<p id="demo"></p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12" style="font-size:15px;">
<p>Drag and drop example- drag the below image on the textbox</p>
<div id="targetDiv" ondrop="drop(event)" ondragover="allowDrop(event)" style="width:400px;height:150px;padding:10px;border:1px solid #aaaaaa;"></div>
<img id="sourceImage" src="https://www.yiibai.com/static/img/logo.png" alt="yiibai" draggable="true" ondragstart="drag(event)" height="120px">
</div>
</div>
<br>
</div>
<script>
//window.open('http://www.baidu.com','MsgWindow');
function generateConfirmBox()
{
var x;
var r=confirm("Press a button!");
if (r==true)
{
x="You pressed OK!";
}
else
{
x="You pressed Cancel!";
}
document.getElementById("demo").innerHTML=x;
}
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</body>
</html>
Selenium-基础操作的更多相关文章
- selenium基础操作
selenium 1.打开和关闭网页 #!/usr/bin/env python # -*- coding:utf-8 -*- from selenium import webdriver drive ...
- <selenium>selenium基础操作
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...
- python下selenium模拟浏览器基础操作
1.安装及下载 selenium安装: pip install selenium 即可自动安装selenium geckodriver下载:https://github.com/mozilla/ge ...
- selenium基础(下拉菜单操作)
selenium基础(下拉菜单操作) 非select/option元素: 1.触发下拉列表出现 2.等待下拉列表中的元素出现,然后进行选择元素即可. select/option元素: 下拉框操作-Se ...
- selenium 基础(一)
selenium安装 pip install selenium selenium操作浏览器原理 早期selenium 1.0 用的selenium RC, 后来selenum2集合了selenium1 ...
- 【Python爬虫】selenium基础用法
selenium 基础用法 阅读目录 初识selenium 基本使用 查找元素 元素互交操作 执行JavaScript 获取元素信息 等待 前进后退 Cookies 选项卡管理 异常处理 初识sele ...
- Selenium | 基础入门 | 截屏并保存于本地
可先参考 Selenium | 基础入门 | 利用Xpath寻找用户框 核心代码: //截屏操作 File srcFile = ((TakesScreenshot)driver).getScree ...
- web 自动化测试 selenium基础到应用(目录)
第一章 自动化测试前提及整体介绍 1-1功能测试和自动化测试的区别 1-2自动化测试流程有哪些 1-3自动化测试用例和手工用例的区别 1-4 自动化测试用例编写 1-5 selenium的优势以及 ...
- selenium鼠标操作 包含右击和浮层菜单的选择
感谢http://www.cnblogs.com/tobecrazy/p/3969390.html 博友的分享 最近在学习selenium的一些鼠标的相关操作 自己在百度的相关操作代码 /** * ...
- selenium基础-跳过验证码
selenium基础-跳过验证码 一.方法 设置万能验证码或者屏蔽验证码(最常用的方法) 使用验证码识别工具识别验证码 通过selenium操作cookies 直接使用配置文件的webdriver 二 ...
随机推荐
- Redis使用文档一
1 Redis概述 1.1前言 Redis是一个开源.支持网络.基于内存亦可持久化的日志型.键值对存储数据库.使用ANSI C编写.并提供多种语言的API. 其开发由VMware主持,是最流行的键值对 ...
- SSL/TLS协议运行机制的概述(转)
互联网的通信安全,建立在SSL/TLS协议之上. 本文简要介绍SSL/TLS协议的运行机制.文章的重点是设计思想和运行过程,不涉及具体的实现细节.如果想了解这方面的内容,请参阅RFC文档. 一.作用 ...
- [Javascript] Format console.log with CSS and String Template Tags
The Chrome console allows you to format messages using CSS properties. This lesson walks you through ...
- QQ欢乐斗地主心得体会 (三):高倍场攻略
上个月,也可能是上个月的上个月,我的欢乐豆兵积攒到了百万之众,突破了历史记录.最近2个月一直在高倍场混,想写点高倍场的心得体会.本篇的体会,与以前写的2篇斗地主心得体会,有点不同. 特别说明:我主要玩 ...
- 【hdu5692】Snacks
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submissio ...
- JAVA类(下)
我看完了Java类,与C++相比,复杂了一点.其中有类的嵌套定义即内部类,枚举类等. 我看这两节花了我很多时间.其中有一些概念还是有点难懂. 下面,我详细总结内部类与枚举类. 内部类 内部类的主要作用 ...
- C++基础代码--20余种数据结构和算法的实现
C++基础代码--20余种数据结构和算法的实现 过年了,闲来无事,翻阅起以前写的代码,无意间找到了大学时写的一套C++工具集,主要是关于数据结构和算法.以及语言层面的工具类.过去好几年了,现在几乎已经 ...
- C++ 与 Java 语言对比
1 . Java 是完全封装的,而 C++ 的函数是可以定义在 Class 的外部的.从这里就可以看出 C++ 的 OO 思想不够彻底,至少在封装这一点上. 2. C++ 中有拷贝构造函数,可以把一个 ...
- C# Tuple VS ValueTuple
C# Tuple VS ValueTuple(元组类 VS 值元组) C# 7.0已经出来一段时间了,大家都知道新特性里面有个对元组的优化:ValueTuple.这里利用详尽的例子详解Tuple VS ...
- SpringBoot JPA 专题
Error: Error starting ApplicationContext. To display the auto-configuration report re-run your appli ...