Forums

Unable to download file with Selenium

Hi to all, I have been trying to download a file from a webpage by clicking on the download button. I am using Selenium and the code runs without errors. It even downloads a screenshot and stores it at the desired folder. However, the file that I want to download does not appear anywhere. Here is the code:

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service

downloadpath = "/home/foo/.virtualenvs/bar/static/UPLOAD_FOLDER"
chrome_options = webdriver.ChromeOptions()
chrome_options.w3c = True
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--safebrowsing-disable-download-protection")
chrome_options.add_argument("window-size=1440,900")

chrome_options.add_experimental_option("prefs", {
        "download.default_directory": downloadpath,
        "directory_upgrade": True,
        "extensions_to_open": "",
        "download.prompt_for_download": False,
        "download.safebrowsing.enabled": True
})


chrome_options.add_experimental_option('w3c', True)
browser = webdriver.Chrome(options=chrome_options)

try:
    browser.get('https://data.buenosaires.gob.ar/dataset/sistema-unico-atencion-ciudadana')
    print(browser.title)
    node = WebDriverWait(browser, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "btn")))
    for n in node:
        print(n.text)
    node[-1].click()
    browser.get_screenshot_as_file("/home/foo/.virtualenvs/bar/static/UPLOAD_FOLDER/download_screenshot.png")
    time.sleep(50)

except Exception as e:
            s = str(e)
            s = "{} - Algo anda mal - linea 46".format(s)
            print(s)
finally:
    browser.quit()

[edit by admin: formatting]

We've had some users report this issue in the past and updating their system image to "haggis" seems to have done the trick. You could try that: https://help.pythonanywhere.com/pages/ChangingSystemImage

Thank you for the quick reply, but updating to haggis did not solve the issue. Any ideas?

It is throwing the following. Message: element is not attached to the page document(Session infoheadless chrome=78.0.3904.70)

That seems like a different issue. It looks like the button is not on the page. Maybe print out the HTML and see what is on the page.

Actually that message was shown only once and might have been because I changed the script. The code does not show any errors, and screen prints properly storing the png image in the correct folder. I have updated to haggis and still ...

Sorry, I'm not sure I understand -- it sounds like you are still having problems but you say (I think) that it's storing the PNG image in the correct folder. Is it working correctly? Or is something still going wrong?

Hi Giles, no, it is not downloading the file while the button is clicked. So, I am still having the problem.

Are you able to make a screenshot? Maybe the download requires extra confirmation or is not actually being clicked on?

Yes, I was able to take a screen shot and store it in a folder. That works well but, either the button is not clicked hence the file is not downloaded or for some reason is not storing the downloaded file. And the thing is that shows no errors.

OK. I finally got it working. However, not with this account but with another one. It worked in Python 3.10. I do not know if it was the Python version or maybe the system image in the other account. Honestly I thought I updated the system image but I might have not done it properly. Thanks a lot for the replies.

Glad to hear it finally worked for you!