title: Python. Selenium. How to wait for new window opens?
body: <p>I have a button with twitter, and after click new window opens, but before opening there is a timout in few seconds, so is there a way to wait for it? I mait in a bad way for now...</p>
  
  <pre><code>    Ui.click_el(link.W9FormNodes.TWITTER_BUTTON)
      # Wait for new window and switch to it
      time.sleep(3)
      aw = driver.window_handles
      driver.switch_to_window(aw[1])
  </code></pre>
  
  <p>So I need something better, that will wait only that amount of seconds that needs.</p>
  
  <p>I think I solve it in this way. But maybe someone will have comments how to do it better.</p>
  
  <pre><code>    aw = Ui.click_and_wait_for_window(link.W9FormNodes.TWITTER_BUTTON)
      driver.switch_to_window(aw[1])
  </code></pre>
  
  <p>And here is a method that click on button and wait for new window:</p>
  
  <pre><code>    def click_and_wait_for_window(self, node):
          current_value = old_value = self.driver.window_handles
          self.click_el(node)
          while len(current_value) == len(old_value):
              time.sleep(0.05)
              current_value = self.driver.window_handles
          return current_value
  </code></pre>