'How to download a file using Selenium on a Flask app?

I've built a simple app that logs in to a website and downloads a file after a few clicks. When run on my local machine the file downloads fine with no issues, but with the Flask deployment the file doesn't get saved anywhere.

The Python script works a bit like this:

def download_file(url, username, password):
    with webdriver.Chrome(executable_path=os.environ.get('CHROMEDRIVER_PATH'), chrome_options=chrome_options) as driver:
        driver.get(url)

        # Code that logs in to the interface
        # Code that goes to the relevant page and apply filters

        export = driver.find_element_by_xpath("Export_XPATH")
        export.click()
        print("[*] Exported file")

And in my app.py:

@app.route("/download", methods=["GET", "POST"])
def download():
    if request.method == "POST":
        return Response(
            download_file(URL, USERNAME, PASSWORD),
            mimetype='text/csv',
            headers={"Content-disposition": f"attachment; filename=report_raw.csv"}
            )
    return render_template('download.html')

The HTML is trivial but I'll attach it below anyway:

download.html
<form action="", method="post">
    <div class="row">
        <div class="col d-flex justify-content-center">
            <input type="submit" value="Download Data" class="input-group-text">
        </div>
    </div>
</form>

Right now it just downloads an empty csv - probably because the download_file() function doesn't return the file, but downloads it from the last .click().

Here's a few things that I've tried:

  1. Define a specific save directory by using chrome_options.add_experimental_option() - the Flask logs show that even though the directory is correct, the file is nowhere to be found. Something about ephemeral storage that I can't wrap my head around...
  2. Use the requests library to get the response by finding the download endpoint + copying in headers and using the drivers.get_cookies() method for cookies. Got the error CONNECTION ERROR: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

I'm very new to programming and open to any and all suggestions! My constraints are that if possible, I'd prefer not to use a storage solution like S3 (but if this is the only option, I'd like to know that too).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source