Selenium wait for download?

Stefan Kendall picture Stefan Kendall · Oct 12, 2010 · Viewed 16k times · Source

I'm trying to test the happy-path for a piece of code which takes a long time to respond, and then begins writing a file to the response output stream, which prompts a download dialog in browsers.

The problem is that this process has failed in the past, throwing an exception after this long amount of work. Is there a way in selenium to wait-for-download or equivalent?

I could throw in a Thread.sleep, but that would be inaccurate and unnecessarily slow down the test run.

What should I do, here?

Answer

Moohebat picture Moohebat · Aug 20, 2015

I had the same problem. I invented something to solve the problem. A tempt file is created by Python with '.part' extension. So, if still we have the temp, python can wait for 10 second and check again if the file is downloaded or not yet.

 while True:
        if os.path.isfile('ts.csv.part'):
            sleep(10)
        elif os.path.isfile('ts.csv'):
            break
        else:
            sleep(10)
 driver.close()