定位用的html素材有两个

demo.html

<html>
<head>
<title>UI Automation Testing</title>
</head>
<style type="text/css">
table{font-size:20;text-align:center;width:80%;border-collapse:collapse;margin:auto}
table th{background: #ccc}
.display{color:red}
.widgetStyle{text-align:left;padding-left:100px}
</style>
<script>
function display_alert(){
alert("I am an alert box!!")
}
function show_div(){
setTimeout("create_div()", 3000);
}
function create_div(){
d = document.createElement('div');
d.className = "red";
d.style.color='red';
d.innerHTML="wait for display";
document.getElementById("display").appendChild(d);
}
function mouseOver(){
document.getElementById("over").innerHTML="<div class='display'>Hello World!</div>";
}
function mouseOut(){
document.getElementById("over").innerHTML="";
}
</script>
<body>
<div>
<div class="text" style="font-size:36;text-align:center"><b>UI Automation Testing</b></div>
<br/>
<table cellpadding="10" cellspacing="0" border="1">
<thead>
<tr>
<th width=20% class="widgetleft">Widget Name</th>
<th width=80% class="widgetStyle">Widget Operate</th>
</tr>
</thead>
<tbody>
<tr>
<td>Input</td>
<td class="widgetStyle"><div id='input'><input type="text" id="user"/></div></td>
</tr>
<tr>
<td>Link</td>
<td class="widgetStyle"><div id='link'><a href="http://www.baidu.com" class='baidu'>baidu</a></div></td>
</tr>
<tr>
<td>Select</td>
<td class="widgetStyle"><div id='select'>
<select name='select'>
<option value ="volvo">Volvo</option>
<option value ="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</div>
</td>
</tr>
<tr>
<td>RadioBox</td>
<td class="widgetStyle">
<div id='radio'>
<input type='radio' name="identity" class='Volvo'/><label>Volvo</label></br>
<input type='radio' name="identity" class='Saab'/><label>Saab</label></br>
<input type='radio' name="identity" class='Opel'/><label>Opel</label></br>
<input type='radio' name="identity" class='Audi'/><label>Audi</label>
</div> </td>
</tr>
<tr>
<td>CheckBox</td>
<td class="widgetStyle">
<div id='checkbox'>
<input type="checkbox" name="checkbox1"/><label>Volvo</label></br>
<input type="checkbox" name="checkbox2"/><label>Saab</label></br>
<input type="checkbox" name="checkbox3"/><label>Opel</label></br>
<input type="checkbox" name="checkbox4"/><label>Audi</label>
</div>
</td>
</tr>
<tr>
<td>Button</td>
<td class="widgetStyle"><div id='button'><input type='button' class='button' value='Submit' disabled="disabled"/></div></td>
</tr>
<tr>
<td>Alert</td>
<td class="widgetStyle"><div id='alert'><input type='button' class='alert' value='Alert' onclick='display_alert()'/></div></td>
</tr>
<tr>
<td>Upload</td>
<td class="widgetStyle"><div id='upload'><input type='file' id='load'/></div></td>
</tr>
<tr>
<td>Open New Window</td>
<td class="widgetStyle"><div id='open'><a href="http://baidu.com" class='open' target='_bank'>Open new window</a></div></td>
</tr>
<tr>
<td>Action</td>
<td class="widgetStyle"><div id='action'>
<input type='button' class='over' value='Action'onmouseover="mouseOver()" onmouseout="mouseOut()"/><div id='over'></div>
</div>
</td>
</tr>
<tr>
<td>Wait</td>
<td class="widgetStyle">
<div id='wait'><input type='button' class='wait' value='Wait' onclick = "show_div()"/></div>
<div id='display'></div>
</td>
</tr>
<tr>
<td>Iframe</td>
<td class="widgetStyle">
<iframe width=800 height=330 name=aa frameborder=0 src="./demo1.html"></iframe>
</td>
</tr>
</tbody> </table>
<br/>
</div>
</body>
</html>

demo1.html

<html>
<head>
<title>UI Automation Testing</title>
</head>
<style type="text/css">
table{font-size:20;text-align:center;width:80%;border-collapse:collapse;margin:auto}
table th{background: #ccc}
.display{color:red}
.widgetStyle{text-align:left;padding-left:100px}
</style>
<script>
function display_alert(){
alert("I am an alert box!!")
}
function show_div(){
setTimeout("create_div()", 5000);
}
function create_div(){
d = document.createElement('div');
d.className = "red";
d.style.color='red';
d.innerHTML="wait for display";
document.getElementById("display").appendChild(d);
}
function mouseOver(){
document.getElementById("over").innerHTML="<div class='display'>Hello World!</div>";
}
function mouseOut(){
document.getElementById("over").innerHTML="";
}
</script>
<body>
<div>
<div class="text" style="font-size:36;text-align:center"><b>UI Automation Testing</b></div>
<br/>
<table cellpadding="10" cellspacing="0" border="1">
<thead>
<tr>
<th width=20% class="widgetleft">Widget Name</th>
<th width=80% class="widgetStyle">Widget Operate</th>
</tr>
</thead>
<tbody>
<tr>
<td>Iframe:Input</td>
<td class="widgetStyle"><div id='input'><input type="text" id="user"/></div></td>
</tr>
<tr>
<td>Iframe:Link</td>
<td class="widgetStyle"><div id='link'><a href="http://www.baidu.com" class='baidu'>baidu</a></div></td>
</tr>
<tr>
<td>Iframe:Select</td>
<td class="widgetStyle"><div id='select'>
<select name='select'>
<option value ="volvo">Volvo</option>
<option value ="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Iframe:RadioBox</td>
<td class="widgetStyle">
<div id='radio'>
<input type='radio' name="identity" class='Volvo'/><label>Volvo</label></br>
<input type='radio' name="identity" class='Saab'/><label>Saab</label></br>
<input type='radio' name="identity" class='Opel'/><label>Opel</label></br>
<input type='radio' name="identity" class='Audi'/><label>Audi</label>
</div> </td>
</tr>
<tr>
<td>Iframe:CheckBox</td>
<td class="widgetStyle">
<div id='checkbox'>
<input type="checkbox" name="checkbox1"/><label>Volvo</label></br>
<input type="checkbox" name="checkbox2"/><label>Saab</label></br>
<input type="checkbox" name="checkbox3"/><label>Opel</label></br>
<input type="checkbox" name="checkbox4"/><label>Audi</label>
</div>
</td>
</tr>
<tr>
<td>Iframe:Button</td>
<td class="widgetStyle"><div id='button'><input type='button' class='button' value='Submit' disabled="disabled"/></div></td>
</tr>
<tr>
<td>Iframe:Alert</td>
<td class="widgetStyle"><div id='alert'><input type='button' class='alert' value='Alert' onclick='display_alert()'/></div></td>
</tr>
<tr>
<td>Iframe:Upload</td>
<td class="widgetStyle"><div id='upload'><input type='file' id='load'/></div></td>
</tr>
<tr>
<td>Iframe:Open New Window</td>
<td class="widgetStyle"><div id='open'><a href="http://baidu.com" class='open' target='_bank'>Open new window</a></div></td>
</tr>
<tr>
<td>Iframe:Action</td>
<td class="widgetStyle"><div id='action'>
<input type='button' class='over' value='Action'onmouseover="mouseOver()" onmouseout="mouseOut()"/><div id='over'></div>
</div>
</td>
</tr>
<tr>
<td>Iframe:Wait</td>
<td class="widgetStyle">
<div id='wait'><input type='button' class='wait' value='Wait' onclick = "show_div()"/></div>
<div id='display'></div>
</td>
</tr>
</tbody> </table>
<br/>
</div>
</body>
</html>

定位代码:

代码1:

package info.milan.webdriver;
import java.io.File;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class day4 {
public WebDriver Driver;
public void startFirefox(){
Driver = new FirefoxDriver();
//最大化
Driver.manage().window().maximize();
}
public void closeFirefox(){
Driver.close();
Driver.quit();
}
public void goTo(){
File f = new File("html/demo.html");
Driver.navigate().to(f.getAbsolutePath());
}
//输入框定位
public void testInput(String str){
///对于文本框的操作,都使用sendkeys来操作
Driver.findElement(By.xpath(".//*[@id='user']")).sendKeys(new String[]{str});
//Driver.findElement(By.xpath(".//*[@id='user']")).clear();
}
//超链接定位
public void testLink(){
Driver.findElement(By.xpath("//a[@class='baidu']")).click();
}
//下拉框定位
public void testOption(){
WebElement element= Driver.findElement(By.cssSelector("select[name='select']"));
Select select = new Select(element);
select.selectByValue("audi");
select.selectByIndex(1);
select.selectByVisibleText("Audi");
System.out.println(select.getFirstSelectedOption().getText());
List<WebElement> options= select.getOptions();
int optionsSize = options.size();
System.out.println("options一共有多少个:"+optionsSize);
for(int i =0;i<optionsSize;i++){
select.selectByIndex(i);
}
}
//单选框定位
public void testRadio(){
//name定位
List<WebElement> elements= Driver.findElements(By.name("identity"));
WebElement el= elements.get(2);
el.click();
boolean isSelect = el.isSelected();
System.out.println("是否被选中:"+elements.get(3).isSelected());
}
//定位复选框
public void testCheckBox(){
//xpath定位
List<WebElement> elements= Driver.findElements(By.xpath(".//div[@id='checkbox']/input"));
elements.get(2).click();
// elements.get(0).click();
for (WebElement e : elements){
if (!e.isSelected()){
e.click();
}
}
}
public void testButton(){
WebElement element= Driver.findElement(By.xpath(".//div[@id='alert']/input"));
element.click();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
day4 t = new day4();
t.startFirefox();
t.goTo();
t.testInput("你好");
//t.testLink();
t.testCheckBox();
t.testRadio();
} }

代码2:

package info.milan.webdriver;
import java.io.File;
import java.util.Set; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class day5 {
public WebDriver Driver;
public void startFirefox(){
Driver = new FirefoxDriver();
Driver.manage().window().maximize();
}
public void closeFirefox(){
Driver.close();
Driver.quit();
}
public void goTo(){
File f = new File("html/demo.html");
Driver.navigate().to(f.getAbsolutePath());
}
//模拟点击弹出窗alert的确定。
public void testAlert(){
WebElement element= Driver.findElement(By.className("alert"));
//element.click();
Actions action = new Actions(Driver);
action.click(element).perform();
//获取当前弹出的alert窗
Alert alert = Driver.switchTo().alert();
String alertMessage = alert.getText();
System.out.println(alertMessage);
///模拟点击alert确认按钮
alert.accept();
}
public void testAction(){
WebElement element= Driver.findElement(By.className("over"));
Actions action = new Actions(Driver);
//将鼠标移动到Action的按钮上面
action.moveToElement(element).perform();
//输出鼠标移动到action按钮上面后显示的文字。
System.out.println( Driver.findElement(By.id("over")).getText());;
}
//上传
public void testUpload(){
WebElement element= Driver.findElement(By.id("load"));
element.sendKeys("C:\\Users\\Administrator\\Desktop\\123.xls");
}
//运行一段js
public void testJs(){
JavascriptExecutor javascript =(JavascriptExecutor)Driver;
javascript.executeScript("alert('你好!')"); }
//对iframe中元素的操作
public void testIframe(){
Driver.findElement(By.id("user")).sendKeys("my test"); WebElement element = Driver.findElement(By.name("aa"));
Driver.switchTo().frame(element);
//Driver.switchTo().frame("aa");
//Driver.switchTo().frame(0);
Driver.findElement(By.id("user")).sendKeys("iframe test"); Driver.switchTo().defaultContent();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Driver.findElement(By.id("user")).sendKeys("my end test");
}
//对多个窗口进行切换操作
public void testMultiWindow(){
WebElement element = Driver.findElement(By.className("open"));
element.click();
Set<String> handles = Driver.getWindowHandles();
String handle = Driver.getWindowHandle(); //得到默认最初窗体句柄 handles.remove(handle);
String newHandle = handles.iterator().next();
print(newHandle);
print(handle);
Driver.switchTo().window(newHandle);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Driver.findElement(By.id("kw")).sendKeys("myString");
//Driver.close();
Driver.switchTo().window(handle);
Driver.findElement(By.id("user")).sendKeys("new test"); }
//获取延时信息,常用于操作ajax请求返回的信息
public void testWait(){
WebElement element = Driver.findElement(By.className("wait"));
element.click();
//方法1 用Thread.sleep,设置了10秒,只能10秒后才能获取数据。
// try {
// Thread.sleep(6000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//Driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//方法2 用WebDriverWait,设置了10秒 如果数据5秒就返回的话,5秒就能获取到数据
boolean wait = new WebDriverWait(Driver,10).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.className("red")). isDisplayed();}
}); print(String.valueOf(wait) );
WebElement elementRed = Driver.findElement(By.className("red"));
String str = elementRed.getText();
print(str); }
public void print(String str){
System.out.println(str); } public static void main(String[] args) {
// TODO Auto-generated method stub
day5 t = new day5();
t.startFirefox();
t.goTo();
// t.testMultiWindow();
// t.testWait();
//t.testAction();
//t.testUpload();
t.testWait();
} }

selenium2入门 定位 窗体切换等等 (二)的更多相关文章

  1. selenium2 定位 窗体切换等等 (二)

    定位用的html素材有两个 demo.html <html> <head> <title>UI Automation Testing</title> & ...

  2. Selenium2入门(二)WebDriver

    前文Selenium2入门(一)说到Selenium是Web 应用程序测试框架,那么如果对一个简单的web应用需求:打开浏览器,登录百度首页,输入“欧洲杯”关键词,点击搜索按钮 这一系列操作,能否用S ...

  3. 如何在Visual Studio 2017中使用C# 7+语法 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构 构建NetCore应用框架之实战篇系列 构建NetCore应用框架之实战篇(一):什么是框架,如何设计一个框架 NetCore入门篇:(十二)在IIS中部署Net Core程序

    如何在Visual Studio 2017中使用C# 7+语法   前言 之前不知看过哪位前辈的博文有点印象C# 7控制台开始支持执行异步方法,然后闲来无事,搞着,搞着没搞出来,然后就写了这篇博文,不 ...

  4. Python_selenium之窗口切换(二)

    Python_selenium之窗口切换(二)一.思路拆分1. 之前有介绍窗口切换,这里加上断言部分2. 这里还是以百度新闻为例,获取百度新闻网址http://news.baidu.com/3. 同样 ...

  5. selenium中的元素操作之三大切换(二)

    一.窗口切换 使用方法: 1.获取到打开的所有的窗口,句柄handles all_handles = driver.window_handles print(all_handles) 2.获取当前的窗 ...

  6. c# winform中窗体切换后释放及防止重复生成

    问题1:窗体切换后如何关闭,并释放资? c# winform中,2个窗体,form1和form2,互相切换的时候执行 this.Hide(); Form2 form2 = new Form2(); f ...

  7. selenium之 定位以及切换frame(iframe)

    Set<String> windows = driver.getWindowHandles();        int count = 0;        for(String handl ...

  8. 一天带你入门到放弃vue.js(二)

    接下来我们继续学习一天带你入门到放弃系列vue.js(二),如有问题请留言讨论! v-if index.html <div id="app"> <p v-if=& ...

  9. Spring Cloud 入门 之 Ribbon 篇(二)

    原文地址:Spring Cloud 入门 之 Ribbon 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Cloud 入门 之 Eureka ...

随机推荐

  1. lua三底漆:lua转让c/c++库(动态链接模式)

    dll按功能luaL_openlib出口,然后lua使用package.loadlib导入库函数,基本就是这么个过程,以下上代码来说明一切. #include "stdafx.h" ...

  2. AIX6.1/11.2.0.3在有关数据库SWAP一个BUG

    昨天南京到客户服务数据库的优化调整,其中新上线,经过审查alert.log当日志现在是在过去一段时间内取得,每隔几个小时的时间滞后,班会报似的内容: Thu Aug 21 09:01:26 2014 ...

  3. The Swift Programming Language中国完整版

    近来的ios的swift语言似乎火了.我没有objectc基金会,但在此之前有c随着java在...的基础上.从几天开始学习ios的swift语言,晚发表在博客形式.下面是英文版本翻译swif图书.还 ...

  4. 使用reserve要再次避免不必要的分配

     关于STL容器,最了不起的一点是,它们会自己主动增长以便容纳下你放入当中的数据,仅仅要没有超出它们的最大限制就能够.对于vector和string,增长过程是这样来实现的:每当须要很多其它空间时 ...

  5. ABP应用层——参数有效性验证

    ABP应用层——参数有效性验证 基于DDD的现代ASP.NET开发框架--ABP系列之17.ABP应用层——参数有效性验证 ABP是“ASP.NET Boilerplate Project (ASP. ...

  6. C# Windows Schedule task此次收购task下一步执行时间

    最近进行了一次需求和Windows Schedule task相关职能,通过schedule,计算下一次运行task时间. 它用于由第三方DLL实现,以下网站,以下载来自: http://tasksc ...

  7. android 环境使用smack 必须注冊的组件

    // Private Data Storage pm.addIQProvider("query", "jabber:iq:private",new Privat ...

  8. C#:winform项目在win7,xp32位和64位都能执行

    vs中项目配置管理器活动解决方式平台选择X86平台.

  9. hdu 3449 (有依赖的01背包)

    依赖背包 事实上,这是一种树形DP,其特点是每个父节点都需要对它的各个儿子的属性进行一次DP以求得自己的相关属性. fj打算去买一些东西,在那之前,他需要一些盒子去装他打算要买的不同的物品.每一个盒子 ...

  10. vim_编码配置文件_utf8乱码解决

    1:简化步奏 调整 :vimrc的脚本配置 编辑~/.vimrc文件,(没有的话新建个)加上如下几行: set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb23 ...