title: How can selenium web driver get to know when the new window has opened and then resume its execution
body: <p>I am facing an issue in automating a web application using selenium web driver.</p>
  
  <p>The webpage has a button which when clicked opens a new window. When I use the following code, it throws <code>OpenQA.Selenium.NoSuchWindowException: No window found</code> </p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>WebDriver.FindElement(By.Id("id of the button that opens new window")).Click();
  //Switch to new window
  _WebDriver.SwitchTo().Window("new window name");
  //Click on button present on the newly opened window
  _WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click();
  </code></pre>
  
  <p>To solve the above issue I add <code>Thread.Sleep(50000);</code> between the button click and <code>SwitchTo</code> statements.</p>
  
  <pre class="lang-js prettyprint-override"><code>WebDriver.FindElement(By.Id("id of the button that opens new window")).Click();
  Thread.Sleep(50000); //wait
  //Switch to new window
  _WebDriver.SwitchTo().Window("new window name");
  //Click on button present on the newly opened window
  _WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click();
  </code></pre>
  
  <p>It solved the issue, but I do not want to use the <code>Thread.Sleep(50000);</code> statement because if the window takes more time to open, code can fail and if window opens quickly then it makes the test slow unnecessarily. </p>
  
  <p>Is there any way to know when the window has opened and then the test can resume its execution?</p>