title: Appium: failed to locate open application error
body: <p>I am using C#, Appium, and WinAppDriver to automate UI tests on a desktop application, however, I continue to have an intermittent issue when launching the WPF application. I have done a lot of research on this but cannot find a solid solution.</p>
  <p>When opening the application there is a login splash screen. When the correct login credentials are entered there is another loading splash screen for approximately 5 seconds before the application opens.</p>
  <p>I continue to get the following exception when opening the application; it appears to be random. Every time I think I found a pattern I find another data point that conflict with the pattern I thought I identified.</p>
  <p><strong>Exception</strong></p>
  <pre><code> Message: 
  OpenQA.Selenium.WebDriverException : Failed to locate opened application window with appId: C:\Program Files (x86)\path to .exe\, and processId: 14124
  
  Stack Trace: 
  RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
  RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
  AppiumDriver`1.Execute(String driverCommandToExecute, Dictionary`2 parameters)
  RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
  RemoteWebDriver.ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
  AppiumDriver`1.ctor(Uri remoteAddress, ICapabilities appiumOptions)
  WindowsDriver`1.ctor(Uri remoteAddress, AppiumOptions AppiumOptions)
  </code></pre>
  <p><strong>Login code</strong></p>
  <pre><code>            if (session == null){
                   AppiumOptions appOptions = new AppiumOptions();
                   appOptions.AddAdditionalCapability(&quot;app&quot;, Path);
                   appOptions.AddAdditionalCapability(&quot;platformName&quot;, &quot;Windows&quot;);
                   appOptions.AddAdditionalCapability(&quot;deviceName&quot;, &quot;WindowsPC&quot;);
                   Thread.Sleep(TimeSpan.FromSeconds(5));
              
                   session = new WindowsDriver&lt;WindowsElement&gt;(new Uri(WindowsApplicationDriverUrl), appOptions);
                   Assert.IsNotNull(session);
                   Thread.Sleep(TimeSpan.FromSeconds(10));
                   var allWindowHandles = session.WindowHandles;
                   session.SwitchTo().Window(allWindowHandles[0]);
                   session.Manage().Timeouts().ImplicitWait = TimeSpan.FromMinutes(1);
  
                   //Enter password and login
                   session.FindElementByAccessibilityId(&quot;tbPwd&quot;).SendKeys(&quot;password&quot;);
                   session.FindElementByAccessibilityId(&quot;btnLogin&quot;).Click();
           
                   Thread.Sleep(TimeSpan.FromSeconds(5));
                   session = new WindowsDriver&lt;WindowsElement&gt;(new Uri(WindowsApplicationDriverUrl), appOptions);
                   session.Manage().Timeouts().ImplicitWait = TimeSpan.FromMinutes(1);
              }
  </code></pre>
  <p><strong>Tear down code after each test case</strong></p>
  <pre><code>public void TearDown()
              {
              AppiumOptions appOptions = new AppiumOptions();
              appOptions.AddAdditionalCapability(&quot;app&quot;, &quot;Root&quot;);
              session = new WindowsDriver&lt;WindowsElement&gt;(new Uri(WindowsApplicationDriverUrl), appOptions);
  
              // Use the session to control the desktop
              session.Keyboard.PressKey(Keys.Command + &quot;r&quot;);
              session.Keyboard.SendKeys(Keys.Null);
              session.Keyboard.SendKeys(@&quot;C:\path of taskkill file.bat&quot;+ Keys.Enter);
  
              //End current session instance
              session.Dispose();
              }
  </code></pre>