ElementNotAvailableException while using application.GetWindows()

Nov 17, 2010 at 12:26 PM

While retreiving windows using application.GetWindows() method:

var windowstemp = (from window in MainApplication.GetWindows() where window.Title == "Export to..." select window).ToList();

sometimes i receive next exception: ElementNotAvailableException (the target element corresponds to UI that is no longer available (for example, the parent window has closed). 

Any ideas how to resolve this problem&

 

Coordinator
Nov 23, 2010 at 12:52 PM

This is a known issue, it would be fixed in next release.

Apr 26, 2012 at 11:18 AM

I am getting the same exception above in my tests. The tests sometimes pass and other times fails with that exception, making my tests a bit fragile. Has there been any solution or work around to this problem yet?

Apr 26, 2012 at 11:33 AM

Hi I have written a little operation to get rid of this problem:

 

 /// <summary>
    /// Gets the windows.
    /// </summary>
    /// <param name="count">The count.</param>
    /// <returns></returns>
    public static List<Window> GetWindows(int count = 0)
    {
      List<Window> windows;
      try
      {
        windows = MyApplication.GetWindows();
      }
      catch (ElementNotAvailableException)
      {
        Thread.Sleep(600);
        if (count < 5)
        {
          windows = GetWindows(count + 1);
        }
        else
        {
          throw;
        }
      }

      return windows;
    }

Hope that helps

 

Throndorin

Apr 29, 2012 at 5:07 PM

That was a great help. It works now like magic. Thanks a lot.