Search code examples
pythonlistseleniumxpathwebdriverwait

Download files with selenium


I'm trying to download individual downloads for each pdf in this page, one download per article. Everyday the number of articles may change

https://diariooficial.elperuano.pe/Normas

I'm kinda new using selenium so I'm not sure how to do it, thanks!

I tried this

chrome_driver = webdriver.Chrome("chromedriver.exe") chrome_driver.get("https://diariooficial.elperuano.pe/Normas") a_list = chrome_driver.find_elements_by_link_text("Descarga individual") for a in a_list: a.click() time.sleep(3)


Solution

  • What you can do here is:
    Wait for all the download button elements appearing (the page is loaded).
    Get the list of all downloading bottons.
    Iterate over these elements clicking them one by one with some pause between these clicks to make downloading performed properly.
    The following code works, I tested it.

    import time
    
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(service=webdriver_service, options=options)
    
    url = 'https://diariooficial.elperuano.pe/Normas'
    driver.get(url)
    wait = WebDriverWait(driver, 20)
    
    buttons = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//article//a[contains(.,'Descarga')]")))
    for button in buttons:
        button.click()
        time.sleep(1)