Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait till download is complete in Python+Selenium+Firefox

I want to close browser as soon file download is complete. I have below code but it's not closing browser. I must be wrong somewhere. Please help me.

driver.find_element_by_link_text("[Comma-Delimited Text (CSV)]").click()
while True:
    if os.path.isfile('C:\\Python34\\*.part'):
        time.sleep(10)
    elif os.path.isfile('C:\\Python34\\*.csv'):
        break
    else:
        time.sleep(10)


def tearDown(self):
    self.driver.quit()
    self.assertEqual([], self.verificationErrors)
like image 346
SSG Avatar asked Jun 28 '26 00:06

SSG


2 Answers

os.path.isfile() does not support glob-style path definitions leading to the loop never exiting.

You need the glob.glob() or fnmatch instead:

  • https://stackoverflow.com/a/4296148/771848

You can also use modules like watchdog to monitor changes in a directory:

  • python selenium, find out when a download has completed?
like image 91
alecxe Avatar answered Jun 30 '26 14:06

alecxe


I have written a download method for zip file download. this could be helpful. after clicking on download button this method will be called and waiting until the download complete

def downloader() :
    print("File Download Started.......")
    sleep(60)
    while True:
        file = glob.glob(BASE_FILE_DIR+'*.zip.part')
        if file:
            print("Download Pending.......")
            sleep(60)
            continue
        else:
            print('File Downloaded')
            break
like image 27
sharif_42 Avatar answered Jun 30 '26 12:06

sharif_42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!