首页 技术 正文
技术 2022年11月20日
0 收藏 613 点赞 3,056 浏览 2240 个字

selenium webdriver学习(六)————如何得到弹出窗口

在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id、name的时候。当时还整理了处理了几种方法,详见:http://seleniumcn.cn/read.php?tid=791 。在selenium webdriver中得到新开窗口相对简单的多,它无关新开窗口的id、name等属性。以下面的html为例:

  1. <span style=”white-space: normal; background-color: rgb(255, 255, 255); “>test.html</span>
  2. <html>
  3. <head><title>Test Popup Window</title></head>
  4. <body>
  5. <a id = “51” href = “http://www.51.com/” target = “_blank”>Let’s go!</a>
  6. </body>
  7. </html>

下面的代码演示了如何去得到弹出的新窗口

  1. import java.util.Iterator;
  2. import java.util.Set;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.firefox.FirefoxDriver;
  6. public class PopupWindowTest {
  7. /**
  8. * @author gongjf
  9. */
  10. public static void main(String[] args) {
  11. System.setProperty(“webdriver.firefox.bin”,”D:\\Program Files\\Mozilla Firefox\\firefox.exe”);
  12. WebDriver dr = new FirefoxDriver();
  13. String url =”\\Your\\Path\\to\\main.html”;
  14. dr.get(url);
  15. dr.findElement(By.id(“51”)).click();
  16. //得到当前窗口的句柄
  17. String currentWindow = dr.getWindowHandle();
  18. //得到所有窗口的句柄
  19. Set<String> handles = dr.getWindowHandles();
  20. Iterator<String> it = handles.iterator();
  21. while(it.hasNext()){
  22. String handle = it.next();
  23. if(currentWindow.equals(handle)) continue;
  24. WebDriver window = dr.switchTo().window(handle);
  25. System.out.println(“title,url = “+window.getTitle()+”,”+window.getCurrentUrl());
  26. }
  27. }
  28. }

输出结果:

  1. title,url = 51.com 真人配对玩游戏,http://www.51.com/

捕获或者说定位弹出窗口的关键在于获得弹出窗口的句柄。(句柄,我的理解是浏览器窗口的一个唯一标识,记得以前玩”按键精灵”也有这玩样。)

在上面的代码里,使用windowhandle方法来获取当前浏览器窗口的句柄,使用了windowhandles方法获取所有弹出的浏览器窗口的句柄,然后通过排除当前句柄的方法来得到新开窗口的句柄。

在获取新弹出窗口的句柄后,使用switchto.window(newwindow_handle)方法,将新窗口的句柄作为参数传入既可捕获到新窗口了。

如果想回到以前的窗口定位元素,那么再调用1次switch_to.window方法,传入之前窗口的句柄既可达到目的。

———————————————————-2102年6月20日——————————————————

PS:今天发现while里的代码有些问题。由原来的:

  1. while(it.hasNext()){
  2. if(currentWindow ==  it.next()) continue;
  3. WebDriver   window = dr.switchTo().window(it.next());
  4. System.out.println(“title,url = “+window.getTitle()+”,”+window.getCurrentUrl());
  5. }

更改为:

  1. while(it.hasNext()){
  2. String handle = it.next();
  3. if(currentWindow.equals(handle)) continue;
  4. WebDriver   window = dr.switchTo().window(handle);
  5. System.out.println(“title,url = “+window.getTitle()+”,”+window.getCurrentUrl());
  6. }

更改原因:

循环里面有两次it.next,多取了一次。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844