python selenium, find out when a download has completed?
There is no built-in to selenium way to wait for the download to be completed.
The general idea here would be to wait until a file would appear in your "Downloads" directory.
This might either be achieved by looping over and over again checking for file existence:
- Check and wait until a file exists to read it
Or, by using things like watchdog
to monitor a directory:
- How to watch a directory for changes?
- Monitoring contents of files/directories?
Selenium (Python) - waiting for a download process to complete using Chrome web driver
You can get the status of each download by visiting chrome://downloads/
with the driver.
To wait for all the downloads to finish and to list all the paths:
def every_downloads_chrome(driver):
if not driver.current_url.startswith("chrome://downloads"):
driver.get("chrome://downloads/")
return driver.execute_script("""
var items = document.querySelector('downloads-manager')
.shadowRoot.getElementById('downloadsList').items;
if (items.every(e => e.state === "COMPLETE"))
return items.map(e => e.fileUrl || e.file_url);
""")
# waits for all the files to be completed and returns the paths
paths = WebDriverWait(driver, 120, 1).until(every_downloads_chrome)
print(paths)
Was updated to support changes till version 81.
Is there a Selenium wait until download complete?
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")
Wait till download is complete in Python+Selenium+Firefox
os.path.isfile()
does not support glob-style path definitions leading to the loop never exiting.
You need the glob.glob()
or fnmatch
instead:
- https://stackoverflow.com/a/4296148/771848
You can also use modules like watchdog
to monitor changes in a directory:
- python selenium, find out when a download has completed?
Related Topics
Python Format Size Application (Converting B to Kb, Mb, Gb, Tb)
How to Verify If a Button Is Enabled and Disabled in Webdriver Python
How to Convert Datetime by Removing Nanoseconds
How to Remove Parentheses from a String
How to Remove Square Brackets from List in Python
How to Make a Roll the Dice Command With My Discord Bot
Spark Equivalent of If Then Else
Removing Backslashes from a String in Python
How to Limit the User Input to Only Integers in Python
How to Kill a While Loop With a Keystroke
How to Determine If My Python Shell Is Executing in 32Bit or 64Bit
How to Make a Function Change Variables While in a While Loop
How to Convert Python Code to Application
Pandas Dataframe Calculations With Previous Row
Python: Fastest Way to Compare Arrays Elementwise
I Want to Reshape 2D Array into 3D Array