2016-01-21 84 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和所有内容,因为代码对我来说看起来很好。 –