Closing all the opened chrome windows using selenium - python - osx
Question: I have to download some files from a website, I'm using Python - Selenium -
Chrome - Osx.

I have his code so far:

    
    
    lnk = "www.foobar.com"
    CHROMEDRIVER=webdriver.Chrome()
    options = webdriver.ChromeOptions()
    profile = {"plugins.plugins_list": [{"enabled":False, "name":"Chrome PDF Viewer"}],
            "download.default_directory" : TEMP_DOWNLOAD}
    options.add_experimental_option("prefs",profile)
    driver = webdriver.Chrome(chrome_options = options)
    driver.get(lnk)
    while True:
        if filter(os.path.isfile, glob.glob(TEMP_DOWNLOAD+'/*.crdownload')):
            pass
        else:
            break
    driver.quit()
    

This code starts the download of the file, waits the end of the download and
then closes the webdriver.

Everything is working properly except that it opens 2 Chrome windows, one to
open the link and the other to download the file, and the quit() method closes
only the latter.

Is there a way to kill all the windows opened by Selenium (I'm trying to avoid
firing a terminal command to kill the processes brute force)?

EDIT:

as Mukesh Takhtani said in comment in my code the problem is a pointless
webdriver instance.


Answer: Use this. This I used for Firefox. You can use this for Chrome. Call
kill_waste() in your python code and it would kill idle useless Chrome. Please
note that this would work for OSX or FreeBSD. For Linux distros you would have
to change the way you are going to use grep and cut

    
    
    import commands
    
    def kill_waste():
    
        (_,firefox_processes) = commands.getstatusoutput("ps -ax | grep '/usr/local/bin/firefox -foreground' | cut -c1-24")
        sleep(0.5)
        firefox_processes = firefox_processes.splitlines()
    
        for pid in firefox_processes:
            values = pid.split()
            time_value = values[3].split(':')
    
            if ((values[2] == 'I' or values[2] == 'I+') and time_value[0] == '1') or time_value[0] == '2':
                commands.getstatusoutput("kill -9 " + values[0])
    

