wwj123
wwj123

Reputation: 375

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()

Upvotes: 4

Views: 12875

Answers (2)

Angia
Angia

Reputation: 335

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)

Upvotes: 7

undetected Selenium
undetected Selenium

Reputation: 193058

Presumably you are trying to invoke click() on the element with text as Data from the ETA 9047 section and to achieve that you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

  • Using XPATH:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_options = webdriver.ChromeOptions() 
    chrome_options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://oui.doleta.gov/unemploy/DataDownloads.asp")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//th[text()='ETA 9047']//following::table[1]//tr/td/a[@title='Data']"))).click()
    
  • Browser Snapshot:

9047

PS: Ensure that you are using Selenium v3.141.59 with ChromeDriver / Chrome v76.0

Upvotes: 0

Related Questions