Search code examples
pythonseleniumselenium-webdriverbrowserwebdriver

Python 3.5 - Selenium - How to handle a new window and wait until it is fully loaded?


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.

Here's my 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])

I get this error from time to time:

newNewWindow = newWindow[1]

IndexError: list index out of range

which make me think that a simple 'time.sleep(5)' doesn't do the work.

So, my question is, how can I wait until this new window is fully loaded before interacting with it?

thanks


Solution

  • You can try to use following code to wait until new window appears:

    WebDriverWait(driver, 20).until(EC.number_of_windows_to_be(2))
    

    Your code should looks like

    Button.click()
    
    WebDriverWait(driver, 20).until(EC.number_of_windows_to_be(2))
    
    newWindow = driver.window_handles
    newNewWindow = newWindow[1]
    driver.switch_to.window(newNewWindow)
    

    Considering @JimEvans comment, try below:

    current = driver.window_handles[0]
    Button.click()
    
    WebDriverWait(driver, 20).until(EC.number_of_windows_to_be(2))
    
    newWindow = [window for window in driver.window_handles if window != current][0]
    driver.switch_to.window(newWindow)