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 二 ...
随机推荐
- HOOK API入门之Hook自己程序的MessageBoxW(简单入门)
说到HOOK,我看了很多的资料和教程,无奈就是学不会HOOK,不懂是我的理解能力差,还是你们说的 不够明白,直到我看了以下这篇文章,终于学会了HOOK: http://blog.sina.com.cn ...
- php实现 句子逆序(需求才是最好的老师)
php实现 句子逆序(需求才是最好的老师) 一.总结 一句话总结:需求才是最好的老师. 1.str_split()和explode()的区别? explode — 使用一个字符串分割另一个字符串 3 ...
- Android图表和图形创建库:EazeGraph
EazeGraph是一个 Android 库用于创建漂亮和花哨的图表.它具有易于使用和高度可定制的特点.当前支持四种不同的图表如下: Chart types Bar Chart Stacked Bar ...
- 以Graphicslayer为管理组来管理Element.
转自原文 以Graphicslayer为管理组来管理Element. 前言 在AE开发过程中,我们经常使用Element(元素).它的出现让地图与用户之间的交互增加了不少的效果.在地图上,可以通过各种 ...
- [javase学习笔记]-7.5 构造函数须要注意的几个细节
这一节我们简单的说几个在使用构造函数时须要注意的细节. 通过我们前几节的学习,我们对构造函数有了一个比較清楚的认识,当我们在创建对象时.我们会调用构造函数.那么我们在定义和调用构造函数时.须要注意哪些 ...
- Python将被加入高考科目?你怎么看?
今天看到这样的一则新闻:不禁感叹,人工智能这股风来的太快,已经掀起全民学习Python的浪潮. 2017年中观察:看上去这个大纲内容基本是这样了,但是实行年份可能要往后推了,不在2017年执行了(据说 ...
- 《erlang程序设计》学习笔记-第3章 分布式编程
http://blog.csdn.net/karl_max/article/details/3985382 1. erlang分布式编程的基本模型 (1) 分布式erlang:这种模型可以让我们在一个 ...
- 选择性编译代码:如 #ifdef __IPHONE_7_0
选择性编译代码: 选择性编译代码和选择性运行代码是不一样的,区别在于: 1.选择性编译代码是在硬件或者系统不支持的情况下不会对该段代码进行编译,也就不会由于不兼容的问题导致报错 #import < ...
- Linux硬件信息查询命令
系统 uname -a # 查看内核/操作系统/CPU信息 Linux hostname 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 ...
- Swift学习——Swift解释具体的基础(六)
Optionals 可选 可选(它似乎并不如此翻译)它适用于那些值这种情况可能是空的,有两种情况一个可选:存在值并等于x,要么值不存在. 选配的概念在OC和C里面并没有.在OC中最接近的概念就是 ...