Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Can't download file using selenium chromewebdriver - 'Failed - Download error'

I was trying to download a file from google chrome using selenium. The code I used below was working fine. But somehow it didn't work anymore. Any ideas?

import os.path
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select

RAWDATA_URL = 'https://oui.doleta.gov/unemploy/DataDownloads.asp'
options = webdriver.ChromeOptions() 
prefs = {'download.default_directory' : SAVE_PATH, "download.prompt_for_download": False}
options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(executable_path = DRIVE_PATH, chrome_options = options)


driver.get(RAWDATA_URL)
time.sleep(5)

the xpath below is just copying from the HTML so should be correct

driver.find_element_by_xpath("//*[@id='main']/table[38]/tbody/tr[2]/td[5]/a").click()

I also tried the get method:

driver.get("https://oui.doleta.gov/unemploy/csv/ar9047.csv")

I was expecting the csv file could download successfully. But google chrome just tell me that "Fail- Download error'.

UPDATE: The question above is simplified by me. There are actually two steps in my project. First downloading the data from one site and then navigating to another to download the csv data.

import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC



SUMMARY_URL = "https://oui.doleta.gov/unemploy/reemploy.asp"
RAWDATA_URL = 'https://oui.doleta.gov/unemploy/DataDownloads.asp'
REEMPLOYMENT_QTR = '09/30/2018' 

options = webdriver.ChromeOptions() 
prefs = {'download.default_directory' : SAVE_PATH, "download.prompt_for_download": False}
options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(executable_path = DRIVE_PATH, chrome_options = options)

First Step:

driver.get(SUMMARY_URL)
time.sleep(5)

select = Select(driver.find_element_by_id('qtr'))
select.select_by_value(REEMPLOYMENT_QTR)
driver.find_element_by_xpath("//input[@name='submit'][@type='submit']").click()
re_table = driver.find_element_by_xpath("//*[@id='content']/table")

state = []
value = []
for re in re_table.find_elements_by_tag_name('tr'):
    c = 0 
    for ele in re.find_elements_by_tag_name('td'):
        if c == 0:
            state.append(ele.text.encode('utf8'))
            c += 1
        else:
            value.append(ele.text.encode('utf8'))


reemployment = pd.DataFrame({'state' : state, AS_OF_DATE : value})
reemployment = reemployment[['state', AS_OF_DATE]]

Second Step(my original question):

driver.execute_script("window.open('');") 
time.sleep(5)
driver.switch_to.window(driver.window_handles[1]) 
time.sleep(5)
driver.get(RAWDATA_URL)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//th[text()='ETA 9047']//following::table[1]//tr/td/a[@title='Data']"))).click()
like image 993
wwj123 Avatar asked Oct 19 '25 00:10

wwj123


1 Answers

my problem is my save path for default directory has issue: it was 'C:/Users/...' but should have been 'C:\Users\...' like below

    chrome_options = webdriver.ChromeOptions()
    prefs = {
    'download.default_directory': 'C:\\Users\\<username>\\Documents\\test\\',
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing_for_trusted_sources_enabled": False,
    "safebrowsing.enabled": False
    }
    chrome_options.add_experimental_option('prefs', prefs)
like image 141
Angia Avatar answered Oct 20 '25 16:10

Angia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!