简体   繁体   中英

How can I download a file using Selenium?

I want to download a xlsx file from a website. And to do that I have to click on a button and I can't find it with my Python code. With my current Python code I can open the website using Selenium and open the download page and what I want is to click on "Excel: extension XLSx" button.

options=webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver=webdriver.Chrome(executable_path=path_web_crawler+"chromedriver.exe",chrome_options=options)
driver.get("https://www.ine.es/jaxiT3/Datos.htm?t=25171")
time.sleep(3)
driver.find_element_by_xpath("//input[@name='btnDescargaForm']").click()

What I tried to detect the button

elem=driver.find_element_by_xpath("//input[@value='xlsx']")
driver.execute_script("arguments[0].click();",elem)
<iframe title="Popup content - Rising content" id="thickBoxINEfrm" name="thickBoxINEfrm" frameborder="0" width="100%" height="100%" marginheight="0" marginwidth="0" tabindex="0" onload=";autoResizeThickBox(this)"></iframe>

Your element is in an iframe.

Import

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

Full Code:

driver.get("https://www.ine.es/jaxiT3/Datos.htm?t=25171")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@name='btnDescargaForm']"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"thickBoxINEfrm")))
elem=wait.until(EC.presence_of_element_located((By.XPATH,"//input[@value='xlsx']")))
driver.execute_script("arguments[0].click();",elem)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM