WebDriverAPI(8)
判断页面元素是否存在
测试网址
http://www.baidu.com
Java语言版本API实例
@Test
public void testIsElementPresent(){
driver.manage().window().maximize();
driver.get(url);
if(IsElementPresent(By.id("kw"))){
WebElement searchInputBox = driver.findElement(By.id("kw"));
if(searchInputBox.isEnabled() == true){
searchInputBox.sendKeys("百度首页搜索框成功找到");
}
}
else{
Assert.fail("未找到页面元素");
}
}
private boolean IsElementPresent(By by) {
try {
//判断传入的参数是否找到页面元素,找到则返回true
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
使用title属性识别和操作弹出的浏览器窗口
测试页面代码
<html>
<head>
<title>你喜欢的水果</title>
</head>
<body>
<p id='p1'>你爱吃的水果么?</P>
<br><br>
<a href="http://www.baidu.com" target="_blank">百度搜索</a>
</body>
</html>
http://www.baidu.com
Java语言版本API实例
@Test
public void identifyPopUpWindowByTitle() {
driver.manage().window().maximize();
driver.get(url);
//获取当前窗口句柄
String parentWindowHandle = driver.getWindowHandle();
WebElement baiduLink = driver.findElement(By.xpath("//a"));
baiduLink.click();
//定义一个set容器存储所有的窗口句柄
Set<String> allWindowsHandles = driver.getWindowHandles();
//如果存储容器对象不为空则进行遍历
if(!allWindowsHandles.isEmpty()){
for(String windowHandle:allWindowsHandles){
try {
//通过窗口的浏览器名称判断是否为百度
if(driver.switchTo().window(windowHandle).getTitle().equals("百度一下,你就知道"))
driver.findElement(By.id("kw")).sendKeys("百度的浏览器窗口被找到");
} catch (NoSuchWindowException e) {
e.printStackTrace();
}
}
}
//返回最早打开的窗口
driver.switchTo().window(parentWindowHandle);
//通过标题断言判断是否为之前窗口
Assert.assertEquals(driver.getTitle(), "你喜欢的水果");
}
使用页面的文字内容识别和处理新弹出的浏览器窗口
测试页面代码
<html>
<head>
<title>你喜欢的水果</title>
</head>
<body>
<p id='p1'>你爱吃的水果么?</P>
<br><br>
<a href="http://www.baidu.com" target="_blank">百度搜索</a>
</body>
</html>
Java语言版本API实例
@Test
public void identifyPopUpWindowByPageSource() {
String parentWindowHandle = driver.getWindowHandle();
driver.get(url);
WebElement baiduLink = driver.findElement(By.xpath("//a"));
baiduLink.click();
Set<String> allWindowHandles = driver.getWindowHandles();
if(!allWindowHandles.isEmpty()){
for(String windowHandle:allWindowHandles){
try {
//判断页面源码中是否包含百度一下4字
if(driver.switchTo().window(windowHandle).getPageSource().contains("百度一下"))
driver.findElement(By.id("kw")).sendKeys("找到百度浏览器窗口");
} catch (NoSuchWindowException e) {
e.printStackTrace();
}
}
}
//返回之前的浏览器窗口
driver.switchTo().window(parentWindowHandle);
Assert.assertEquals(driver.getTitle(), "你喜欢的水果");
}
WebDriverAPI(8)的更多相关文章
- WebDriverAPI(7)
查看页面元素的属性 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void getWebElementAttribute() { dri ...
- WebDriverAPI(10)
操作Frame页面元素 测试网址代码 frameset.html: <html> <head> <title>frameset页面</title> &l ...
- WebDriverAPI(9)
操作JavaScript的Alert窗口 测试网址代码 <html> <head> <title>你喜欢的水果</title> </head> ...
- WebDriverAPI(4)
单击某个元素 采用元素id.click()方法即可 双击某个元素id.doubleClick 操作单选下拉列表 测试网页HTML代码 <html> <body> <sel ...
- WebDriverAPI(2)
操作浏览器窗口 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com" ...
- WebDriverAPI(6)
在指定元素上方进行鼠标悬浮 测试网址 http://www.baidu.com Java语言版本实例 @Test public void roverOnElement() { driver.manag ...
- WebDriverAPI(5)
将当前浏览器截屏 测试网址 http://www.baidu.com Java语言版本实例 @Test public void captureScreenInCurrentWindows() { dr ...
- WebDriverAPI(3)
获取页面的Title属性 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com& ...
- WebDriverAPI(1)
访问某网页地址 被测网址http:http://www.baidu.com Java语言版本的API实例代码 方法一: @Test public void visitURL(){ String bas ...
随机推荐
- using directive 使用指令,与using declaration使用声明。
使用指令是把名字空间中的所有名字引入到当前作用域,而使用声明是把名字空间的某个名字引入到当前作用域中 语法如下 //test.cpp #include<iostream> //using ...
- 2018.08.20 loj#115. 无源汇有上下界可行流(模板)
传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...
- hdu-1087(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:每确定一个数,后面一个数肯定比它大.所以可以先从最后一个数开始,不断向前确定前面的状态,推 ...
- phoneGap,angularJs,onSen的一些备忘
1.ng-click="funcName";这里的funcName需要再控制器里的$scope.funcName=function(){}进行定义 2.ng-controller= ...
- python类的继承-1
#!/usr/bin/python3 #类定义 class people: #定义基本属性 name = '' age = 0 #定义私有属性,私有属性在类外部无法直接进行访问 __weight = ...
- 命令行生成war包
1.找到自己的代码位置 2.进入cmd界面 3.进入对应的目录 4.执行命令 5.就会开始自动打包 6.在文件夹下生成对应的war包
- POJ3045 Cow Acrobats 2017-05-11 18:06 31人阅读 评论(0) 收藏
Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4998 Accepted: 1892 Desc ...
- shell 脚本 删除文件内容为空的文件
#!/bin/bask # cd /tmp for a in * ;do if [ ! -s $a ] ;then #[ ! -s $a ] 文件为空返回为真 rm -rf $a fi done 测试 ...
- mysql中设置默认字符编码为utf-8
使用过Linux的同志就知道,在Linux下安装mysql,尤其是使用yum安装的时候,我们是没法选择其默认的字符编码方式.这个就是一个比较头痛的问题,如果Linux数据库中使用到中文的时候,乱码问题 ...
- What is Pay Me to Learn——Google Summer of Code 2013
原文链接:http://zhchbin.github.io/2013/10/17/what-is-pay-me-to-learn/ 背景 今天早上才想起来,自己还欠着一件事情没有做完.很久在人人上之前 ...