Process hangs if web browser crashes in selenium
Question: I am using selenium + python, been using implicit waits and try/except code on
python to catch errors. However I have been noticing that if the browser
crashes (let's say the user closes the browser during the program's
executing), my python program will hang, and the timeouts of implicit wait
seems to not work when this happens. The below process will just stay there
forever.

    
    
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    from selenium import webdriver
    import datetime
    import time
    import sys
    import os
    
    def open_browser():
        print "Opening web page..."
    
        driver = webdriver.Chrome()
    
        driver.implicitly_wait(1)
        #driver.set_page_load_timeout(30)
        return driver
    
    
    driver = open_browser() # Opens web browser
    
    # LET'S SAY I CLOSE THE BROWSER RIGHT HERE! 
    # IF I CLOSE THE PROCESS HERE, THE PROGRAM WILL HANG FOREVER
    time.sleep(5)
    while True:
        try:
            driver.get('http://www.google.com')
            break
       except:
            driver.quit()
            driver = open_browser()
    


Answer: The code you have provided will always hang in the event that there is an
exception getting the google home page. What is probably happening is that
attempting to get the google home page is resulting in an exception which
would normally halt the program, but you are masking that out with the except
clause.

Attempt with the following amendment to your loop.

    
    
    max_attemtps = 10
    attempts = 0
    while attempts <= max_attempts:
        try:
            print "Retrieving google"
            driver.get('http://www.google.com')
            break
       except:
            print "Retrieving google failed"
            attempts += 1
    

