title: Python 3.5 - Selenium - How to handle a new window and wait until it is fully loaded?
body: <p>I am doing browser automation and I am blocked at a certain point: at a moment, I ask the browser to click on a button, which in turn open up a new window. But sometimes the Internet is too slow, and so this new window takes time to load. I want to know how can I ask Selenium to wait until this new fresh window is fully loaded.</p>
  <p>Here's my code:</p>
  <pre><code>driver.switch_to.default_content()
  Button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'addPicturesBtn')))
  Button.click()
  newWindow = driver.window_handles
  time.sleep(5)
  newNewWindow = newWindow[1]
  driver.switch_to.window(newNewWindow)
  newButtonToLoad = driver.find_element_by_id('d')
  newButtonToLoad.send_keys('pic.jpg')
  uploadButton = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'uploadPics')))
  uploadButton.click()
  driver.switch_to.window(newWindow[0])
  </code></pre>
  <p>I get this error from time to time:</p>
  <blockquote>
  <p>newNewWindow = newWindow[1]</p>
  <p>IndexError: list index out of range</p>
  </blockquote>
  <p>which make me think that a simple 'time.sleep(5)' doesn't do the work.</p>
  <p><strong>So, my question is, how can I wait until this new window is fully loaded before interacting with it?</strong></p>
  <p>thanks</p>