2016-01-21 82 views
1

我面臨完全相同的問題描述hereSelenium腳本暫停切換窗口句柄

但它是一個關閉的線程。 即時通訊使用硒webdriver 2.48.2,在win7 IE 11. 情況是這樣的,我有一個測試,點擊一個按鈕,應該打開一個新的實驗,這個新的實驗打開新的標籤在鉻和Firefox上的相同標籤,但在IE11上運行硒時會在新窗口中打開。但奇怪的是,當手動打開瀏覽器而不是通過硒腳本打開新窗口時。 也許硒腳本打開新的webdriver?並在搜索新頁面的元素時停止腳本。代碼做的是檢查新的句柄是否被打開,找到新的句柄,然後將窗口句柄切換到新的句柄。 這裏是C#代碼片段。

private static TResult TriggerAndWaitForNewWindow<TResult>(PageObject pageObject, Action action, int timeout = 30) 
    where TResult : PageObject, new() 
{ 
    IParent parent = pageObject.Driver; 
    List<String> existingHandles = pageObject.Driver.WindowHandles.ToList(); 
    action(); 

    string popupHandle = Wait.Until(() => 
    { 
    string foundHandle = null; 
    List<string> currentHandles = pageObject.Driver.WindowHandles.ToList(); 
    var differentHandles = GetDifference(existingHandles, currentHandles); 

    if (differentHandles.Count > 0) 
    { 
     Boolean hasSomeLength = differentHandles[differentHandles.Count-1].Length > 1; 

     if (hasSomeLength) 
     foundHandle = differentHandles[differentHandles.Count - 1];  
    } 
    return foundHandle; 
    }, "Waiting for new Window Handle to appear", timeout, 2000); 

    // Init the new page object but override the window handle 
    TResult page = PageObject.Init<TResult>(parent); 
    page.WindowHandle = popupHandle; 

    page.SwitchToMyWindow(); 
    return page; 
} 

private static List<String> GetDifference(List<string> existingHandles, List<string> currentHandles) 
{ 
     System.Threading.Thread.Sleep(15000); 
     return currentHandles.Except(existingHandles).ToList(); 
} 

止步這個函數內部對IE11

public Boolean SwitchToMyWindow() 
{  
    try 
    { 
    String windowHandle = this.WindowHandle; // must be the old handle 
    try 
    { 
     if (this.Driver.CurrentWindowHandle == windowHandle) 
     { 
     Log.Info("No need to cswitch window"); 
     return true; 
     } 
    } 
    catch(Exception e) 
    { 
     Log.Warn("We have no current driver window, must have been closed"); 
    } 

    Log.Info("Switching to Window Handle {0}", this.Driver.CurrentWindowHandle); 
    this.Driver.SwitchTo().Window(windowHandle); <---- Halts here on IE11 
    //Pause.milliSeconds(500); 
      Boolean switched = Wait.Until(() => 
       this.Driver.CurrentWindowHandle == windowHandle, "Waiting for my window handle to be the active one", 5, 1000); 
     } 
    catch (OpenQA.Selenium.WebDriverTimeoutException tEx) 
    { 

    } 
    return true; 
} 

沒有其他人遇到過這個問題?如何解決它?

回答

1

您可以驗證Selenium是否支持您的目標操作系統? 您的目標操作系統可能並未完全支持Selenium。

請查看以下鏈接瞭解更多詳情。 http://grokbase.com/t/gg/webdriver/1267fdkgaa/openqa-selenium-nosuchwindowexception-with-ie9-and-windows-2008

+1

aaah,我沒有考慮到該平臺可能是一個問題。我會得到同樣的錯誤,這很奇怪,很煩人。我嘗試更新Java版本,IEDriver和所有內容,因爲代碼對我來說看起來很好。 –