This is the best I could do. One thing to note is that selenium is not really meant for downloading, so I added a sleep at the end to ensure the download completes. It can also be done with scripts to monitor the download status, but I don't really know how to do it. I also had to add a sleep in the middle to make sure the "Maker" click was being captured correctly. I'm sure there is a better way to do that as well.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
with webdriver.Chrome() as driver:
driver.get("https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[id='yaxisVar_label']"))).click()
time.sleep(2)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li[data-label='Maker']"))).click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "j_idt61"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "vchgroupTable:xls"))).click()
time.sleep(10)
You might be able to do this with a requests.post()
. I didn't look into the headers or form data, but it is there.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…