Check and Open Downloaded File in Selenium

How to open a downloaded excel file in selenium

You didn't post any sample code, but anyway, check out this generic example.

import requests
url = "https://www.nordpoolgroup.com/Market-data1/Power-system-data/Production1/Wind-Power-Prognosis/DK/Hourly/?view=table"

r = requests.post(url) #POST Request
with open('data_123.xls', 'wb') as f:
f.write(r.content)

# file goes here, by default, if WD is not changed to somewhere else:
# C:\Users\your_path_here\

File is not getting downloaded using selenium chromedriver

You can try the below CSS_SELECTOR :

a[title^='CSV (comma delimited)'][onclick*='CSV']

Using Explicit waits :

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title^='CSV (comma delimited)'][onclick*='CSV']"))).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

Directly use driver.find_element

driver.find_element(By.CSS_SELECTOR, "a[title^='CSV (comma delimited)'][onclick*='CSV']").click()

If these 2 do not work, try JS :-

button = driver.find_element_by_css_selector("a[title^='CSV (comma delimited)'][onclick*='CSV']")
driver.execute_script("arguments[0].click();", button)

Getting Download Link of Last Downloaded File - Selenium

Things to be noted down.

  1. You'd have to scroll down till the web element is in Selenium view port.
  2. Launch browser in full screen mode.
  3. Use explicit waits.
  4. There's 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)
  5. Also, the desired link is in shadow-root element which has to handle via JS call.

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


Related Topics



Leave a reply



Submit