
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Out When a Download Has Completed Using Python and Selenium
We can find when a download has completed with Selenium webdriver in Python. We shall use the ChromeOptions class for this purpose. First, we shall create an object of the ChromeOptions class.
Then apply the add_experimental_option method on the object created. We shall pass browser preferences and download.default_directory:<location of downloaded file> as parameters to that method. Finally, this information shall be passed to the driver object.
Once the download is completed, we can verify it with the help of the os.path.isfile method. The path of the downloaded file is passed as a parameter to that method. The method os.path.exists shall also be used to verify if the downloaded file path exists.
Syntax
op = webdriver.ChromeOptions() p = {'download.default_directory':'C:\Users\Downloads\Test'} op.add_experimental_option('prefs', p)
Example
from selenium import webdriver from selenium.webdriver.chrome.options import Options import time import os.path #object of ChromeOptions class op = webdriver.ChromeOptions() #browser preferences p = {'download.default_directory': 'C:\Users\Downloads\Test'} #add options to browser op.add_experimental_option('prefs', p) #set chromedriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", options=op) #maximize browser driver.maximize_window() #launch URL driver.get("https://www.seleniumhq.org/download/"); #click download link l = driver.find_element_by_link_text("32 bit Windows IE") l.click() #check if file downloaded file path exists while not os.path.exists('C:\Users\Downloads\Test'): time.sleep(2) #check file if os.path.isfile('C:\Users\Downloads\Test\IEDriverServer_Win32.zip): print("File download is completed") else: print("File download is not completed") #close browser driver.quit()
Output
Advertisements