Home > OS >  Failed Download Error While Using Python and Selenium
Failed Download Error While Using Python and Selenium

Time:04-29

My script downloads a PDF file, but when it downloads, it says "Failed - Download Error".

When I do everything manually, it works fine.

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import pandas as pd, numpy as np
import os
import datetime
from datetime import timedelta
import urllib.request

options = webdriver.ChromeOptions()
options.add_experimental_option(

    'prefs',
    {
        "download.default_directory":"C:/Users/my.username/Desktop",
        "download.prompt_for_download":False,
        "download.directory_upgrade": True,
        "plugins.always_open_pdf_externally": True

    }
)
    

driver = webdriver.Chrome(
    executable_path = 'C:/Users/my.username/Downloads/chromdriver_v100/chromedriver.exe'
    ,chrome_options = options
    )
   
driver.get('https://www.mywebsite/users/sign_in')


WebElement_username = driver.find_element_by_name("user[login]")
WebElement_username.send_keys("my_username")


WebElement_password = driver.find_element_by_name("user[password]")
WebElement_password.send_keys("my_password")

WebElement_password.send_keys(Keys.ENTER)


url = 'https://www.mywebsite.com/company/123/facility/58965/reports/close_of_day/2022-4-1/2022-4-20.pdf'

driver.get(url)

Is there something else I need to do differently?

CodePudding user response:

Figured it out.

Here is some useful documentation:

https://support.google.com/chrome/thread/8871110/failed-download-error-since-chrome-version-75-0-3770-100-official-build-64-bit?hl=en

I needed to change:

"download.default_directory":"C:/Users/my.username/Desktop",

to

"download.default_directory":"C:\Users\my.username\Desktop",
  • Related