Getting the name of file download using selenium in python
Question: So I'm downloading a file using Selenium and it works fine but I need to get
the name of the file.

My variable `path` should get the name of what was downloaded but all it
prints out is "none".

    
    
    driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=Binary)
    driver.get("stuff")
    time.sleep(2)
    path = driver.find_element_by_xpath("//a[contains(text(), 'tgz')]").click()
    print path
    


Answer: This is an alternate solution to
[@TehTris](http://stackoverflow.com/u/2193134)'s
[solution](http://stackoverflow.com/a/26371425/2932244).

It is not necessary that all the links will contain the file name (they can
also be redirects at times), so in that case you can try checking the
difference in the directory listing before and after downloading the file by
using [`os.listdir()`](https://docs.python.org/2/library/os.html#os.listdir).

    
    
    import os
    
    before = os.listdir('/home/jason/Downloads')
    
    # Download the file using Selenium here
    
    after = os.listdir('/home/jason/Downloads')
    change = set(after) - set(before)
    if len(change) == 1:
        file_name = change.pop()
    else:
        print "More than one file or no file downloaded"
    

