title: Switching Windows and Finding Page Objects in Selenium
body: <p>I have an automated script that is using Selenium with the page object model and the C# bindings and requires two windows to run. The site is done in Angular 2, also. What needs to happen is each window needs to be brought to certain point and then a button is hit in both windows in quick succession. The separate windows are being spawned from two separate instances using the Selenium grid. The windows come up just fine and the first switch (switching to the second window when it's spawned) happens seamlessly</p>
  
  <p>Here is the code that generates each window</p>
  
  <p>Window 1:</p>
  
  <pre><code>//start up the browser and initialize any test variables
  driver1 = InitializeTestObjects(testID, true, true);
  winHandle1 = driver1.CurrentWindowHandle;
  </code></pre>
  
  <p>Window 2:</p>
  
  <pre><code>//second browser
  driver2 = InitializeTestObjects(testID, false, true);
  winHandle2 = driver2.CurrentWindowHandle;
  </code></pre>
  
  <p>Where winHandle1 and winHandle2 are the current handles for each window and are grabbed as soon as they are generated</p>
  
  <p>As the script progresses through it steps. it will load page objects from a page object class. Here is the code from the script that loads the page object:</p>
  
  <pre><code>mt2 = new MissionTab(driver2);
  mt2.clickAutomate();
  </code></pre>
  
  <p>And that page object class</p>
  
  <pre><code>public class MissionTab : HomePage
  {
      public MissionTab(IWebDriver driver)
          : base(driver)
      {
          PageFactory.InitElements(driver, this);
      }
  
      #region Page Objects
  
      //private NgWebElement btnOpen = angDriver.FindElement(By.XPath("//*[@id='sts-mission-view-card-footer']/div/div/button[1]"));
      private IWebElement btnAutomate = driver.FindElement(By.XPath("//*[@id='sts-mission-view-card-footer']/div/div/button[2]"));
      private IWebElement chkAutoRefresh = driver.FindElement(By.XPath("//*[@id='sts-mission-view-card-header']/sts-card-header/div/div[2]/div/div/div[1]/div[2]/md-checkbox"));
      private IWebElement tblTaskList = driver.FindElement(By.Id("sts-mission-view-card-content"));
  
      #endregion
      #region Page Methods
      #endregion
      }
  }
  </code></pre>
  
  <p>Here is the behavior I am seeing. At some point during the script when I have both windows to the point where I will need to hit the button being tested, I am getting a NoSuchElement exception after I switch back to the window I am not using. Here's that code:</p>
  
  <pre><code>//switch back to first window
  driver1.SwitchTo().Window(winHandle1)
  
  //load home page object and click the Automate button
  mt1 = new MissionTab(driver1);
  mt1.clickAutomate();
  </code></pre>
  
  <p>But the weird thing is that if I just try and bring the elements I need individually using a dummy IWebElement in  my script, it works just fine. here's the code to accomplish the same operation as above. the switchTo is switching back to the original, but it won't recognize (find) any of the page objects. Using the dummy element, I can find the objects and act on them with no problem and I don't even need the switchTo() statement.</p>
  
  <pre><code>//find the Automate button and click 
  IWebElement ele = driver1.FindElement(By.XPath("//*[@id='sts-mission-view-card-footer']/div/div/button[2]"));
  ele.Click();
  </code></pre>
  
  <p>Why would this be happening? Anyone else noticed this?</p>