3

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()
wwj123
  • 365
  • 2
  • 12

2 Answers2

6

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)
Angia
  • 177
  • 2
  • 4
0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you DebanjanB. I tried the WebDriverWait approach but chrome still tells me the same error message. Interestingly, if I manually click on the "Data" link from the chrome open by webdriver, I still got the same error message. But if I open the chrome myself and navigate to the page manually, then I can click and download successfully.... Weird... – wwj123 Aug 12 '19 at 14:38
  • @CarlosWen Checkout the updated answer and let me know the status. – undetected Selenium Aug 12 '19 at 14:42
  • 1
    Thanks again. I directly used your code provided and it work. I am good now. I guess the reason has to do with opening other tabs before navigating to the page I download that csv file. I post my very initial code and you could take a look if you are interested. And yes, I am using the versions as suggested. Thank you. – wwj123 Aug 12 '19 at 15:24