Skip to content
Advertisement

Python and Selenium download 0 KB excel files

I have already been to this link with same question, but I cannot find an answer to it: Although my question is the same as the other question, I posted a new one with my code as well.

url='https://example.com/'
download_url="https:/example.com/Download"
chromedriver = 'path\tochromedriver.exe'
options = Options()
    ua = UserAgent()
    userAgent = ua.random
    print(userAgent)
    options.add_argument(f'user-agent={userAgent}')
    options.add_experimental_option("prefs", {
        "download.default_directory": r"C:UsersheliaDesktopTest",
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing.enabled": True
    })
    options.add_argument("--headless")
    options.add_argument("--window-size=%s" % WINDOW_SIZE)
driver = webdriver.Chrome(chrome_options=options, executable_path=chromedriver)
 driver.get(url)
user_name = driver.find_element_by_name('User')
pass_word = driver.find_element_by_name("Pass")
user_name.send_keys("my_username")
pass_word.send_keys("my_password")
driver.find_element_by_class_name("btnn.btnn-default.b").click()
driver.get(download_url)
driver.find_element_by_class_name("btn.btn-app").click()
driver.switch_to.alert.accept()

The code successfully downloads the file but the file is 0 KB. both on the website and on my local; however, the file on the site has never been 0 before. (the program finishes while the file is being downloaded, could it be the cause? do I need to add some waits?)

Advertisement

Answer

Your question is not clear enough, however I guess your problem is:
After clicking the download button and accepting the alert your code finishes immediately so downloaded file have had no enough time to be actually downloaded.
In order to get the file completely downloaded you should prevent browser to be closed until the downloading not complete.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement