I have a program that downloads a file using selenium and then gets the file name with os.listdir
.
My problem right now is that the download takes too long, and my code has moved on with the process. How can I pause the code until the file is downloaded?
Is there a selenium wait until download complete, or a way for selenium to pass the downloaded files name into a variable that i can plug into the answer from here
driver.find_element(By.XPATH, '//button[text()="Export to CSV"]').click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Download"]')))
driver.find_element(By.XPATH, '//button[text()="Download"]').click()
files=os.listdir(folderPath)#finds all files in folder
print(files)
The clear answer of you question is No. Selenium don't have such method to wait until download get completed.
You can write your own custom method which check downloaded file name in download directory continuously in some time interval.
def is_file_downloaded(filename, timeout=60):
end_time = time.time() + timeout
while not os.path.exists(filename):
time.sleep(1)
if time.time() > end_time:
print("File not found within time")
return False
if os.path.exists(filename):
print("File found")
return True
Call that method just after code line which hits download.
driver.find_element(By.XPATH, '//button[text()="Download"]').click()
file_path = '/Users/narendra.rajput/Documents/30.docx'
if is_file_downloaded(file_path, 30):
print("yes")
else:
print("No")