selenium webdriver學習(六)------------如何得到彈出窗口


在selenium 1.X里面得到彈出窗口是一件比較麻煩的事,特別是新開窗口沒有id、name的時候。當時還整理了處理了幾種方法,詳見:http://seleniumcn.cn/read.php?tid=791 。在selenium webdriver中得到新開窗口相對簡單的多,它無關新開窗口的id、name等屬性。以下面的html為例:

Html代碼   收藏代碼
  1. <span style="white-space: normal; #ffffff;">test.html</span>  
  2.   
  3.   
  4. <html>  
  5.    
  6.     <head><title>Test Popup Window</title></head>  
  7.    
  8.     <body>  
  9.    
  10.         <id = "51" href = "http://www.51.com/" target = "_blank">Let's go!</a>  
  11.    
  12.     </body>  
  13.    
  14. </html>  

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

 

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

 

 輸出結果:

 

Java代碼   收藏代碼
  1. title,url = 51.com 真人配對玩游戲,http://www.51.com/  
 

 

 

捕獲或者說定位彈出窗口的關鍵在於獲得彈出窗口的句柄。(句柄,我的理解是瀏覽器窗口的一個唯一標識,記得以前玩"按鍵精靈"也有這玩樣。)

在上面的代碼里,使用windowhandle方法來獲取當前瀏覽器窗口的句柄,使用了windowhandles方法獲取所有彈出的瀏覽器窗口的句柄,然后通過排除當前句柄的方法來得到新開窗口的句柄。

在獲取新彈出窗口的句柄后,使用switchto.window(newwindow_handle)方法,將新窗口的句柄作為參數傳入既可捕獲到新窗口了。

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

 

 

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

PS:今天發現while里的代碼有些問題。由原來的:

 

Java代碼   收藏代碼
  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.         }  
 

更改為:

 

Java代碼   收藏代碼
  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,多取了一次。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM