Search code examples
pythonseleniumselenium-webdriverselenium-chromedriverwebautomation

Upload recent downloaded file using Selenium webdriver- PYTHON


I want to upload a file using Selenium whose name I don't know, the only thing I know is that it's in the downloads folder and it's the recent file. How can I do so?


Solution

  • It has nothing to do with selenium. You can use native libraries from python to obtain desired result.

    import os
    import glob
    
    # * matches all files in directory
    # if you need other specific file types
    # use *.[type], for example *.py
    files_in_dir = glob.glob('[path to your folder]/*') 
    recent_file = max(list_of_files, key=os.path.getctime)
    

    os.path.getctime refers to last metadata change time in current path in system. This way you can filter out and get most recent file with recent date.
    glob module is used to find specific file patterns in specified directory. Read more about glob here.
    os module provides some utility capabilites from operating system. More about os here.