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.
- You'd have to
scroll down till the web element
is in Selenium view port. - Launch
browser
in full screen mode. - Use
explicit waits.
- There's
no link in HTML body
, but download is appearing inchrome://downloads
, so we can navigate todownloads
section and then the can a grab a link, (Please see below) - 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
Knowing Which Java.Exe Process to Kill on a Windows Machine
How to Close a Javafx Application on Window Close
Why Do I Get 404 for Rest With Spring-Boot
How to Process a Multipart Request Consisting of a File and a Json Object in Spring Restful Service
How to Identify File Type by Base64 Encoded String of a Image
Java Stream Collectors.Groupingby() Multiple Fields
Could Not Extract Resultset in Hibernate
Spring Junit: How to Mock Autowired Component in Autowired Component
Java: How to Print Array Without Square Brackets and Commas
How to Store a String Longer Than 4000 Characters in an Oracle Database Using Java/Jdbc
How to Split a String, But Also Keep the Delimiters
Generate 6 Digit Random Number
Calculate the Number of Items Displayed by Recyclerview and Place in a Textview
How to Query Using an Enum Parameter Mapped as Ordinal Using JPA and Hibernate