切换窗口句柄时Selenium Script停止

时间:2016-01-21 08:37:15

标签: c# selenium

我面临与here

所述完全相同的问题

但它是一个封闭的线程。 我在win7 IE 11上使用selenium webdriver 2.48.2。 情况是这样的,我有一个测试,点击一个按钮,它应该打开一个新实验,这个新实验在chrome上的新选项卡和firefox上的相同选项卡中打开,但在IE11的新窗口中打开它是通过硒。但奇怪的是,当手动打开浏览器而不是通过selenium脚本时,它不会在新窗口中打开。 也许selenium脚本打开新的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 个答案:

答案 0 :(得分:1)

您能否验证Selenium是否支持您的目标操作系统? 您的目标操作系统可能不完全支持Selenium。

请查看以下链接了解更多详情。 http://grokbase.com/t/gg/webdriver/1267fdkgaa/openqa-selenium-nosuchwindowexception-with-ie9-and-windows-2008