Search code examples
pythonseleniumselenium-webdriverxpathwebdriverwait

How to download PDF by clicking using selenium chrome driver


I am trying to download pdf from https://www.axisbank.com/interest-rate-on-deposits?cta=homepage-rhs-fd link following code only open the website and click on download link but i am not able to download pdf Python code

print("Helllo Pratik")
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory": "D:\\pdf_download", 
"download.prompt_for_download": False, 
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True 
})
driver = webdriver.Chrome('D:\driver_chrome\chromedriver.exe',options=options)
driver.get("https://www.axisbank.com/interest-rate-on-deposits?cta=homepage-rhs-fd")
element=driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div[2]/a/div/div/span")
element.click()
driver.quit()

Solution

  • To download the Domestic Fixed Deposits pdf from the website you need to click on the element inducing WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

    options = Options()
    options.add_argument("start-maximized")
    options.add_experimental_option('prefs', {
    "download.default_directory": "C:\\Datafiles", 
    "download.prompt_for_download": False, 
    "download.directory_upgrade": True,
    "plugins.always_open_pdf_externally": True 
    })
    s = Service('C:\\BrowserDrivers\\chromedriver.exe')
    driver = webdriver.Chrome(service=s, options=options)
    driver.get("https://www.axisbank.com/interest-rate-on-deposits?cta=homepage-rhs-fd")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2[text()='Domestic Fixed Deposits']//following-sibling::div[1]/span"))).click()
    

    Note: You have to add the following imports :

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

    Browser Snapshot:

    fixed deposit