I am trying to get link of Last Downloaded file in Selenium Python. The following is the download Button for downloading. It doesn't show any link.
<button id="download_mi_button" class="btn btn-success btn-pill btn-lg w-50"> Download File</button>
So I thought to download the file and then get the link of the last downloaded file and possibly cancel the downloading too as I just need the link.
Page URL : https://mifirm.net/download/5612
Download Button Text : Download Server Xiaomi
Things to be noted down.
scroll down till the web element
is in Selenium view port.browser
in full screen mode.explicit waits.
no link in HTML body
, but download is appearing in chrome://downloads
, so we can navigate to downloads
section and then the can a grab a link, (Please see below)Code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(50)
wait = WebDriverWait(driver, 20)
driver.get("https://mifirm.net/download/5612")
ele = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "button[id='download_mi_button']")))
driver.execute_script("arguments[0].scrollIntoView(true);", ele)
driver.execute_script("arguments[0].click();", ele)
time.sleep(3)
driver.get("chrome://downloads/")
time.sleep(3)
download_link = driver.execute_script('return document.querySelector("body > downloads-manager").shadowRoot.querySelector("#frb0").shadowRoot.querySelector("#url")')
print(download_link.get_attribute('href'))
Cancel_Button = driver.execute_script('return document.querySelector("body > downloads-manager").shadowRoot.querySelector("#frb0").shadowRoot.querySelector("#safe > div:nth-child(6) > cr-button")')
Cancel_Button.click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC