Back
I've used selenium to initiate a download. After the download is complete, certain actions need to be taken, is there any simple method to find out when a download completed? (I am using the FireFox driver)
I came across this problem recently. I was downloading multiple files at once and had to build in a way to timeout if the downloads failed.
The code checks the filenames in some download directory every second and exits once they are complete or if it takes longer than 20 seconds to finish. The returned download time was used to check if the downloads were successful or if it timed out.
I use this function for times that .crdownload does not appear as the extension. Essentially this just waits for the correct number of files as well.
def download_wait(directory, timeout, nfiles=None): """ Wait for downloads to finish with a specified timeout. Args ---- directory : str The path to the folder where the files will be downloaded. timeout : int // How many seconds to wait until timing out. nfiles : int, defaults to None If provided, also wait for the expected number of files. """ seconds = 0 dl_wait = True while dl_wait and seconds < timeout: time.sleep(1) dl_wait = False files = os.listdir(directory) if nfiles and len(files) != nfiles: dl_wait = True for fname in files: if fname.endswith('.crdownload'): dl_wait = True seconds += 1 return seconds
def download_wait(directory, timeout, nfiles=None):
"""
Wait for downloads to finish with a specified timeout.
Args
----
directory : str
The path to the folder where the files will be downloaded.
timeout : int
// How many seconds to wait until timing out.
nfiles : int, defaults to None
If provided, also wait for the expected number of files.
seconds = 0
dl_wait = True
while dl_wait and seconds < timeout:
time.sleep(1)
dl_wait = False
files = os.listdir(directory)
if nfiles and len(files) != nfiles:
for fname in files:
if fname.endswith('.crdownload'):
seconds += 1
return seconds
31k questions
32.8k answers
501 comments
693 users