zoukankan      html  css  js  c++  java
  • Selenium常用操作汇总二——如何得到弹出窗口

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

    <span style="white-space: normal; #ffffff;">test.html</span>  
      
      
    <html>  
       
        <head><title>Test Popup Window</title></head>  
       
        <body>  
       
            <a id = "51" href = "http://www.51.com/" target = "_blank">Let's go!</a>  
       
        </body>  
       
    </html>  

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

    import java.util.Iterator;  
    import java.util.Set;  
      
    import org.openqa.selenium.By;  
    import org.openqa.selenium.WebDriver;  
    import org.openqa.selenium.firefox.FirefoxDriver;  
      
    public class PopupWindowTest {  
      
      
        /** 
         * @author gongjf 
         */  
        public static void main(String[] args) {  
            System.setProperty("webdriver.firefox.bin","D:\Program Files\Mozilla Firefox\firefox.exe");    
            WebDriver dr = new FirefoxDriver();  
            String url ="\Your\Path\to\main.html";  
            dr.get(url);      
            dr.findElement(By.id("51")).click();  
            //得到当前窗口的句柄  
            String currentWindow = dr.getWindowHandle();  
            //得到所有窗口的句柄  
            Set<String> handles = dr.getWindowHandles();  
            Iterator<String> it = handles.iterator();  
            while(it.hasNext()){  
                String handle = it.next();  
                if(currentWindow.equals(handle)) continue;  
                WebDriver window = dr.switchTo().window(handle);  
                System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());  
            }  
        }  
      
    }  

    输出结果

    title,url = 51.com 真人配对玩游戏,http://www.51.com/  
  • 相关阅读:
    四、django rest_framework源码之频率控制剖析
    Ubuntu14.04配置记录
    尝试开始写博客
    用IDEA把SpringBoot项目打成jar发布项目
    IDEA创建springboot项目部署到远程Docker
    springboot 快速部署
    最详细的 Spring Boot 多模块开发与排坑指南
    SpringMVC的工作原理
    Dubbo最详解
    Zookeeper入门看这篇就够了
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/9207163.html
Copyright © 2011-2022 走看看