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

上一篇 / 下一篇  2012-11-21 15:47:57 / 个人分类:webdriver

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

Html代码  

<!--[if !supportLists]-->1. <!--[endif]--><span style="white-space: normal; background-color: #ffffff;">test.html</span>  

<!--[if !supportLists]-->2. <!--[endif]-->  

<!--[if !supportLists]-->3. <!--[endif]-->  

<!--[if !supportLists]-->4. <!--[endif]--><html>  

<!--[if !supportLists]-->5. <!--[endif]-->   

<!--[if !supportLists]-->6. <!--[endif]-->    <head><title>Test Popup Window</title></head>  

<!--[if !supportLists]-->7. <!--[endif]-->   

<!--[if !supportLists]-->8. <!--[endif]-->    <body>  

<!--[if !supportLists]-->9. <!--[endif]-->   

<!--[if !supportLists]-->10.<!--[endif]-->        <a id = "51" href = "http://www.51.com/" target = "_blank">Let's go!</a>  

<!--[if !supportLists]-->11.<!--[endif]-->   

<!--[if !supportLists]-->12.<!--[endif]-->    </body>  

<!--[if !supportLists]-->13.<!--[endif]-->   

<!--[if !supportLists]-->14.<!--[endif]--></html>  

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

 

Java代码  

<!--[if !supportLists]-->1. <!--[endif]-->import java.util.Iterator;  

<!--[if !supportLists]-->2. <!--[endif]-->import java.util.Set;  

<!--[if !supportLists]-->3. <!--[endif]-->  

<!--[if !supportLists]-->4. <!--[endif]-->import org.openqa.selenium.By;  

<!--[if !supportLists]-->5. <!--[endif]-->import org.openqa.selenium.WebDriver;  

<!--[if !supportLists]-->6. <!--[endif]-->import org.openqa.selenium.firefox.FirefoxDriver;  

<!--[if !supportLists]-->7. <!--[endif]-->  

<!--[if !supportLists]-->8. <!--[endif]-->public class PopupWindowTest {  

<!--[if !supportLists]-->9. <!--[endif]-->  

<!--[if !supportLists]-->10.<!--[endif]-->  

<!--[if !supportLists]-->11.<!--[endif]-->    /** 

<!--[if !supportLists]-->12.<!--[endif]-->     * @author gongjf 

<!--[if !supportLists]-->13.<!--[endif]-->     */  

<!--[if !supportLists]-->14.<!--[endif]-->    public static void main(String[] args) {  

<!--[if !supportLists]-->15.<!--[endif]-->        System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");    

<!--[if !supportLists]-->16.<!--[endif]-->        WebDriver dr = new FirefoxDriver();  

<!--[if !supportLists]-->17.<!--[endif]-->        String url ="\\Your\\Path\\to\\main.html";  

<!--[if !supportLists]-->18.<!--[endif]-->        dr.get(url);      

<!--[if !supportLists]-->19.<!--[endif]-->        dr.findElement(By.id("51")).click();  

<!--[if !supportLists]-->20.<!--[endif]-->        //得到当前窗口的句柄  

<!--[if !supportLists]-->21.<!--[endif]-->        String currentWindow = dr.getWindowHandle();  

<!--[if !supportLists]-->22.<!--[endif]-->        //得到所有窗口的句柄  

<!--[if !supportLists]-->23.<!--[endif]-->        Set<String> handles = dr.getWindowHandles();  

<!--[if !supportLists]-->24.<!--[endif]-->        Iterator<String> it = handles.iterator();  

<!--[if !supportLists]-->25.<!--[endif]-->        while(it.hasNext()){  

<!--[if !supportLists]-->26.<!--[endif]-->            String handle = it.next();  

<!--[if !supportLists]-->27.<!--[endif]-->            if(currentWindow.equals(handle)) continue;  

<!--[if !supportLists]-->28.<!--[endif]-->            WebDriver window = dr.switchTo().window(handle);  

<!--[if !supportLists]-->29.<!--[endif]-->            System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());  

<!--[if !supportLists]-->30.<!--[endif]-->        }  

<!--[if !supportLists]-->31.<!--[endif]-->    }  

<!--[if !supportLists]-->


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-16  
   1234
567891011
12131415161718
19202122232425
262728293031 

我的存档

数据统计

  • 访问量: 5665
  • 日志数: 8
  • 建立时间: 2012-11-14
  • 更新时间: 2012-11-21

RSS订阅

Open Toolbar