2

The python script searches a website for a file and downloads it to a specified location! Everything works fine and the file is downloaded but right when driver.quit() is executed the file is automatically moved to my trash folder. This is my code:

def firefoxOptions():
    options = Options()

    options.headless = True
    options.set_preference("browser.download.folderList", 2)
    options.set_preference("browser.download.manager.showWhenStarting", False)
    options.set_preference("browser.download.dir", "PATH")
    options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/csv")
    return options


def search():
    url = 'site_URL'
    driver = webdriver.Firefox(options=firefoxOptions()) 
    driver.get(url)

    time.sleep(3) #waits for the page to properly load

    driver.find_element(
        By.CSS_SELECTOR,
        "css_selector_first_button").click()

    time.sleep(1) #waits to load

#finds the download button and click it
    driver.find_element(By.CSS_SELECTOR, "css_selector_second_button").click()

    time.sleep(15) #waits for the download to finish
    print("download complete!")
    driver.quit() #file gets deleted when this is executed

search()

I tried using driver.close() instead of driver.quit() but same thing happen!

  • You could try to download the file using other library instead of using the driver. For example you could get the url from the button and use request to download. – Juanje May 28 '20 at 14:20
  • Did you find any solution for this? Im having exactly the same problem with my code and everything seems to be fine – Carlost Apr 06 '22 at 05:06

1 Answers1

1

Are you sure the file completely downloads and that 15 seconds are enough? Look here for that case: python selenium, find out when a download has completed?

Otherwise, since the file was downloaded via an automation script, maybe your antivirus software moved it to trash. Temporarily disabling your antivirus, running your script and seeing if the downloaded file is still moved to trash should determine if it is your antivirus software.

Thierryonre
  • 74
  • 2
  • 4
  • The downloaded files have .part extension at the end so the antivirus moved them. I manually changed .part to .csv before ```driver.quit()``` and it worked! – user9329808 May 28 '20 at 14:26
  • Happy to help :D – Thierryonre May 28 '20 at 14:30
  • .part files are not fully download, are you sure this solution is correct? – Juanje May 28 '20 at 15:05
  • I was thinking about that - maybe the whole csv file was not downloaded and the implementation from the link would work better. – Thierryonre May 28 '20 at 15:34
  • @Juanje The files are only few KB of size, so 15 seconds is more than enough. If I compare an automatically downloaded file to a one I download manually, they are exactly the same! But i don't know why .part is always appended! – user9329808 May 28 '20 at 16:30
  • I am not sure too but I would recommend to download the file with other method just to be sure that the driver is not missing something. – Juanje May 29 '20 at 10:44