3

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)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
SSG
  • 73
  • 1
  • 10

2 Answers2

4

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

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

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

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Just what I wanted, it's very helpful when file download is anywhere between some seconds to some minutes. Thank you so much! – SSG Dec 11 '17 at 19:15
1

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
sharif_42
  • 511
  • 4
  • 11