selenium2.0处理muti-Windows 、 Frames 、Popup Dialogs

selenium2.0处理多窗口,弹窗等,只需要调用WebDriver 嵌套类:TargetLocator(driver.switchTo.……),如下:

driver.switchTo().window("windowName");//target="windowName" ,或者 直接都是使用获取当前窗口句柄来定位

driver.switchTo().frame("frameName");

Alert alert = driver.switchTo().alert();

注:当 driver.switchTo().alert()得到alert后,就可以对alert做accept,dismiss,读alert内容,或者填写prompt,适合于alert/confirm/prompts

下面是具体实例操作代码:

package mavenSelenium;

import java.util.Set;

import org.openqa.selenium.By;
import org.junit.*;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; public class VariableWindows extends Assert { protected WebDriver driver;
@Before
public void setUp(){
driver=new FirefoxDriver();
} @Test
public void test() throws Exception{
driver.get("file:///C:/Users/jennifer.huang/Desktop/AlertConfirmPromptPage.html");
//1、操作confirm弹出框
WebElement e1=driver.findElement(By.id("btnConfirm"));
e1.click();
Alert alert11=driver.switchTo().alert();
if(!alert11.getText().contains("Choose an option.")){
fail("The confirm should contains [Choose an option.]");
}
alert11.accept();
String expectString="Confirmed";
verifyResult(expectString);//验证结果Assert.fail e1.click();
Alert alert12=driver.switchTo().alert();
alert12.dismiss();
expectString="Rejected!";
verifyResult(expectString); //2、操作Alert
WebElement e2=driver.findElement(By.id("btnAlert"));
e2.click();
Alert alert21=driver.switchTo().alert();
if(!alert21.getText().contains("I'm blocking")){
fail("Alert should conatins [I'm blocking]");
}
alert21.accept();
expectString="Alert is gone";
verifyResult(expectString); //3、操作prompt
WebElement e3=driver.findElement(By.id("btnPrompt"));
e3.click();
Alert alert31=driver.switchTo().alert();
expectString="selenium2.0";
alert31.sendKeys(expectString);
alert31.accept();
verifyResult(expectString); //4、操作新Tab
//4.1 打开新tab target="_blank"
WebElement e4=driver.findElement(By.id("linkNewWindow"));
e4.click();
changeFocus("百度一下,你就知道");
System.out.println("当前窗口title:"+driver.getTitle());
//4.2按钮打开新窗口
changeFocus("主窗口");
System.out.println("当前窗口title:"+driver.getTitle());
WebElement e5=driver.findElement(By.id("btnNewNamelessWindow"));
e5.click();
changeFocus("博客园 - 程序员的网上家园");
System.out.println("当前窗口title:"+driver.getTitle());
//4.3按钮打开新窗口
Thread.sleep(5000);
changeFocus("主窗口");
WebElement e6=driver.findElement(By.id("btnNewNamedWindow"));
e6.click();
changeFocus("博客园 - 程序员的网上家园"); //另外selenium2.0其他操作窗口语句有:
driver.get("http://www.google.com");//Load a new web page in the current browser window
driver.navigate().to("http://www.cnblogs.com/jenniferhuang/"); //和driver.get(“url”),都是新载一个页面
Thread.sleep(2000);
driver.navigate().back(); //move backwards in browser’s history:
Thread.sleep(2000);
driver.navigate().forward(); //move forwards in browser’s history: }
@After
public void tearDoown(){
driver.quit();
} /**
* 验证结果
* @param expectString
*/
public void verifyResult(String expectString){
String resultString=driver.findElement(By.id("output")).getText();
if(!resultString.contains(expectString)){
fail("Element [" + By.id("output") + "] should contains [" + expectString + "] ; but now it contains: " + resultString);
}
}
/**
* 窗口跳转, 通过title来确定要跳到哪个窗口
* @param windowTitle
*/
public void changeFocus(String windowTitle){
for(String handle:driver.getWindowHandles()){
if(driver.switchTo().window(handle).getTitle().equals(windowTitle)){
driver.switchTo().window(handle);
break;
}
}
}

上面代码所操作的 页面的源代码示例:AlertConfirmPromptPage.html

<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>主窗口</title>
<script type="text/javascript">
function output(resultText){
document.getElementById('output').childNodes[0].nodeValue=resultText;
} function show_confirm(){
var confirmation=confirm("Choose an option.");
if (confirmation==true){
output("Confirmed.");
}
else{
output("Rejected!");
}
} function show_alert(){
alert("I'm blocking!");
output("Alert is gone.");
}
function show_prompt(){
var response = prompt("What's the best web QA tool?","Selenium");
output(response);
}
function open_window(windowName){
window.open("http://www.cnblogs.com/",windowName);
}
</script>
</head>
<body> <input type="button" id="btnConfirm" onclick="show_confirm()" value="Show confirm box" /></br>
<input type="button" id="btnAlert" onclick="show_alert()" value="Show alert" /></br>
<input type="button" id="btnPrompt" onclick="show_prompt()" value="Show prompt" /> </br>
<a href="http://www.baidu.com" id="linkNewWindow" target="_blank">New Window Link</a></br>
<input type="button" id="btnNewNamelessWindow" onclick="open_window()" value="Open Nameless Window" /></br>
<input type="button" id="btnNewNamedWindow" onclick="open_window('Mike')" value="Open Named Window" />
<br />
<span id="output">
</span>
</body>
</html>

selenium2.0 处理各种窗口问题解决方法的更多相关文章

  1. vc++6.0各种报错合集(附:VC++6.0调出打印窗口的方法)

    背景: 由于VC++6.0对于现在的我来说,只是一个工具,暂时没有太多的时间分配到这块去深究它,由于不明其原理,因此也只是在此把错误积累下来,以备下次相同错误出现时能快速排除,节省时间. 正文 一.出 ...

  2. selenium IDE处理各种窗口问题解决方法

    一.处理模态窗口:showModalDialog 由于弹出模态窗口后,就无法定位到当前窗口的元素和模态窗口的元素,需要添加js解决 模态窗口动作类似下面语句: <input id="c ...

  3. GeoServer 2.15.0版本跨域问题解决方法

    geoserver默认不开启跨域设置,开启步骤如下: 1.修改配置文件web.xml,该配置文件的路径如下 \webapps\geoserver\WEB-INF\web.xml 2.搜索:cross- ...

  4. 初次安装hive-2.1.0启动报错问题解决方法

    首次安装hive-2.1.0,通过bin/hive登录hive shell命令行,报错如下: [hadoop@db03 hive-2.1.0]$ bin/hive which: no hbase in ...

  5. Flask无法访问(127.0.0.1:5000)的问题解决方法

    Flask默认开启的ip地址是:http://127.0.0.1:5000/ 但在运行时可能存在无法访问的问题,特别是当我们在linux服务器上搭建flask时,此时需要将代码修改如下: app.ru ...

  6. win7中VS2010中安装CSS3.0问题解决方法

    win7中VS2010中安装CSS3.0问题解决方法   在安装Standards Update for VS2010 SP1后,VS2010中没有CSS3.0问题,以下是我的解决方法 1.首先去官网 ...

  7. java绘图原理------在窗口界面(或面板上)画出一张或多张图片问题解决方法

    /** *@author blovedr * 功能: java绘图原理------在窗口界面(或面板上)画出一张或多张图片问题解决方法 * 日期: 2018年4月28日     16:20 * 注释: ...

  8. Springmvx拦截html出现406解决以及Server Tomcat v8.0 Server at localhost failed to start 问题解决方法

    问题是这样的:环境是SSM框架,在配置好的框架里想请求一个html,结果406了,406就是HTTP协议状态码的一种,表示无法使用请求的特性来响应请求的网页.一般指客户端浏览器不接受所请求页面的MIM ...

  9. selenium1.0和selenium2.0页面等待处理详解

    一.selenium1.0页面等待 1.……AndWait 经常会看到, selenium action命令中很多有这种……AndWait后缀, 例如click和clickAndWait命令: cli ...

随机推荐

  1. TweenMax动画库学习(三)

    目录               TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)           ...

  2. Bind Enum to ListControl

    当使用MVVM时,相信你和我一样经常有这样的需求: 在ViewModel里定义了一个Enum,它必然是对应UI上的一个ListControl作为不同选项. 有一种做法是使用Converter,将Enu ...

  3. HBase的安装与使用

    1.安装 由于还是学习阶段,所以没有在生产环境练习,就在本地建了个虚拟机进行HBase的安装. 下载地址http://www.apache.org/dyn/closer.cgi/hbase/,选择一个 ...

  4. MVC-Model数据注解(二)-自定义

    由于系统的数据注解肯定不适合所有的场合,所以有时候我们需要自定义数据注解.         自定义数据注解有两种,一种是直接写在模型对象中,这样做的好处是验证时只需要关心一种模型对象的验证逻辑,缺点也 ...

  5. MINA实战

    backlog,放入到acceptor队列中还未处理最大数量: solinger参数,用来说明close之后是否直接关闭:参数越大,等待时间越长. mina中的udp处理,即使对端关闭也会继续传输,但 ...

  6. 解决npm安装模块时 npm err! registry error parsing json

    最近还真是点背,从yeoman生成一个react项目或是github上克隆一个项目,在npm install的时候, 一直报npm err! registry error parsing json(就 ...

  7. 解决Matlab启动时 " Can't check 9.0 VCRTs The application has failed to start because……" 的错误

    注:转载或引用请注明出处 今天在winserver 2012 r2 上安装matlab 2016b , 安装完成运行时提示: ERROR: Cnn't check 9.0 VCRTs <star ...

  8. throw 与 throws的应用

    throws---------->把异常交给调用处. 可以结合throw来同时使用. throws 用在方法声明处,表示本方法不处理异常.可以结合throw使用 throw 表示在方法中手工抛出 ...

  9. .NET(C#):灵活运用CryptoStream,加密不是必须用CryptoStreamMode.Write

    首先.NET中的ICryptoTransform是单向的,也就是只能从一个状态将数据转化成另一个状态,反之是不可以的.当然手动 操作ICryptoTransform还是比较繁琐的,通过CryptoSt ...

  10. [JAVA]各种杂七杂八的东西......

    BigInteger / BigDecimal / string 一些常用的函数: 加 add减 substract乘 multiply除 divid取余 mod / remainder (remin ...