2016-01-19 108 views
1

基本我正在使用Maven與POM,並且我無法在第二個窗口中執行操作並單擊發送窗口關閉後自動關閉父窗口,之後,我的Firefox驅動程序不起作用。自動關閉窗口並切換回硒窗口中的父窗口

**我用低於提到的三個選項不工作**

public void switchWindow() { 
     String handle = driver.getWindowHandle(); 
     Set<String> set = driver.getWindowHandles(); 
     set.remove(handle); 
     if (set.size() >= 1) { 
      driver.switchTo().window((String) set.toArray()[0]); 
     } 


    public WebDriver switchWindowByTitle(String title) 
    { 
     WebDriver popup = null; 
     Set<String> windowIterator = driver.getWindowHandles(); 
     System.err.println("No of windows : " + windowIterator.size()); 
     for (String s : windowIterator) { 
      String windowHandle = s; 
      popup = driver.switchTo().window(windowHandle); 
      System.out.println("Window Title : " + popup.getTitle()); 
      System.out.println("Window Url : " + popup.getCurrentUrl()); 
      if (popup.getTitle().equals(title)){ 
       System.out.println("Selected Window Title : " + popup.getTitle()); 
       return popup; 
      } 

     } 
     System.out.println("Window Title :" + popup.getTitle()); 
     System.out.println(); 
     return popup; 


public WebDriver switchWindowByTitle(String title) 
    { 

String currentWindow = driver.getWindowHandle(); 
    List<String> availableWindows = new ArrayList<String>(driver.getWindowHandles()); 

    for(String w : availableWindows) 
    { 
     if (w != currentWindow) 
     { 
     driver.switchTo().window(w); 
     if (driver.getTitle().equalsIgnoreCase(title)) 
      return true; 
     else 
     { 
      driver.switchTo().window(currentWindow); 
     } 
     } 
    } 
    return false; 
    } 
+0

能否請您參考http://stackoverflow.com/questions/19112209/how-to-handle-the-new-window-in-selenium-webdriver看到解決您的查詢? –

+0

@AbdulHameed: - 我已經提到上面的鏈接,如果你可以看看我的第二個選項是同一個 –

回答

2

您需要打開新的窗口之前得到父窗口的句柄和SWITH回到它要用新窗口結束後, 。

// before opening the new window 
String parentWindow = driver.getWindowHandle(); 

// after the new window was closed 
driver.switchTo().window(parentWindow); 
+0

我已經嘗試了上述建議,它不工作,基本上我使用我自己的網絡驅動程序,請參閱網址下面提供進一步參考 https://www.dropbox.com/s/2avinp6qhxguv96/TestWebDriver.java?dl=0&preview=TestWebDriver.java –

+0

@ChiragGupta彈出關閉後,'driver.getWindowHandles()'有一個窗口,還是它可以識別更多的窗口? – Guy