在webdriver官方的api中,切换窗口的方法提供的参数是nameOrHandle。
引用
WebDriver window(java.lang.String nameOrHandle)
Switch the focus of future commands for this driver to the window with the given name/handle.
但是在实际运用中,更多的可能是需要根据新窗口的title去切换窗口。因此封装了如下方法:
public boolean switchToWindow(WebDriver driver,String windowTitle){
boolean flag = false;
try {
String currentHandle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
for (String s : handles) {
if (s.equals(currentHandle))
continue;
else {
driver.switchTo().window(s);
if (driver.getTitle().contains(windowTitle)) {
flag = true;
System.out.println("Switch to window: "
+ windowTitle + " successfully!");
break;
} else
continue;
}
}
} catch (NoSuchWindowException e) {
System.out.println("Window: " + windowTitle
+ " cound not found!", e.fillInStackTrace());
flag = false;
}
return flag;
}
首先获取当前driver驱动的所有窗口的handle然后进行比对筛选,如果窗口的title符合预期,则切换,并返回true,反之则返回false。
分享到:
相关推荐
selenium webdriver2 环境搭建
selenium RC与selenium webdriver的区别
Selenium WebDriver 3.14.0 本来需要.net45 我改为.net2.0 希望喜欢的人需要。 dll 反编译,嵌入程序内。无需调用那个讨厌的dll。 绝对不骗人,骗人是小狗。架构复杂修改麻烦,各位体谅
selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创
关于Selenium WebDriver工作原理的介绍,原理相关描述
selenium2 webdriver的介绍 selenium2 webdriver的配置安装 selenium2 webdriver的基本操作 selenium2 webdriver使用技巧 ……
Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...
python 自动化测试框架;selenium webdriver python 第三版本
selenium WebDriver 安装包及启动服务的一些命令 对于正在学习自动化测试的小伙伴们非常有用
本书是一本从入门到精通模式的Selenium WebDriver实战经验分享书籍。全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用...
Selenium-Webdriver系列教程 Selenium 1.x时代已经远去,它理应躺在历史的角落里,靠着壁炉烤着火,抽着旱烟,在袅袅的升起的青烟中回忆那曾经属于自己的美好时代。 不过事实却并非如此,现今原本早应退役的...
selenium-webdriver-2.5.0.gem
selenium webdriver2 一个完整demo
selenium webdriver是web自动化的一本经典著作,吴老集合python java不同版本。本资源为java版本,内容较新,无论是入门还是提升都有很大帮助。
测试 官方开发文档测试
Selenium WebDriver Recipes in C#(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
本教程是来源与书 Selenium Testing Tools cookbook,但是其中的所有代码都是通过python来实现并且调试通过的。此文档为版本为粗略版,后期会不断的更新优化
Selenium 客户端和 WebDriver 语言绑定(Selenium Clients and WebDriver Language Bindings)适用于C#语言 为了创建与 Selenium Server (Remote WebDriver) 交互的脚本或创建本地 Selenium WebDriver 脚本,您需要...
个人最近开始学习Selenium,但发现对应python的有用的资料奇少无比,于是 自己努力收集并整理了一些资料 学会搭建环境后的同学,建议先抛弃其他一切资料,先看一遍这个python资料,保证收益匪浅,基本上可以动手做...