
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 & 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
- Related Questions & Answers
- How to Download & Install Selenium WebDriver?
- Using Extensions with Selenium & Python
- Download image with Selenium Python
- How do I automatically download files from a pop up dialog using selenium-python?
- How can I download a file on a click event using selenium?
- C++ Program to find out if a person has won lottery
- How to download all pdf files with selenium python?
- How do I download Selenium RC?
- How do you automatically download a Pdf with Selenium Webdriver in Python?
- Compare keys & values in a JSON object when one object has extra keys in JavaScript
- How to find out who has checked my Facebook profile?
- How to disable Javascript when using Selenium?
- Program to find out if a vertex in an undirected graph has a lesser cost path in Python
- YouTube Media/Audio Download using Python - pafy
- How to download Google Images using Python
Advertisements