Download and check if file exists

Issue

I’m trying to get a similar script. VIDEO
No clue how he did this, working in every browser.
I’m not sure how to grab and check the url filename.

1 - open a text file containing a list with URLs (example.com/file.exe, anotherurl.com/file2.exe)
2 - for every url, open a browser tab, download the file and check if file exists 
3 - Print "file downloaded" else "download failed" or calculate the fail ratio

My code, it worked for a single url when I know the filename. I’ve been trying to make it work for url lists. It should grab the filename in the url path /file.exe

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import os.path
import requests
op = webdriver.ChromeOptions()
p = {'download.default_directory': 'C:\\Users\\VM\\Downloads'}
op.add_experimental_option('prefs', p)
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe",
options=op)
with open("C:\\Users\\VM\\Downloads\\urls.txt",'r') as file:
    for url in file.readlines():

         driver.get(url);
         time.sleep(5)
         if os.path.isfile('CHECK URL PATH FILENAME'):
            print("File download is completed")
         else:
            print("File download is not completed") 

Solution

What you can do is every time before you download file clean a folder(Delete all files) and then after hitting url check if atleast one file is present if file is present you can check file name contains ‘.exe’

     // clean folder by deleting all files from download location folder
     driver.get(url);
    
     time.sleep(5)
     if os.path.isfile('CHECK URL PATH FILENAME'): // get list of files present in folder ane verify if file name ends with .exe
        print("File download is completed") // Write code to delete all files after this line.
     else:
        print("File download is not

Answered By – Ketan Pardeshi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published