import os
import time
import shutil
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException, TimeoutException, NoSuchFrameException, WebDriverException
path = "/Users/nadialucas/Dropbox/epic_task/rawdata/txt"
os.chdir(path)
files = ["demographics.txt","house_age1.txt","house_age2.txt","house_chars1.txt","house_chars2.txt","sample82.txt"]
for f in files:
filepath = os.path.join(path, f)
if os.path.exists(filepath):
os.remove(filepath)
waiting = 10
engine = 'chrome'
chromeOptions = Options()
prefs = {"download.default_directory" : path}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = "/Users/nadialucas/anaconda/bin/chromedriver"
driver = webdriver.Chrome(executable_path = chromedriver, chrome_options = chromeOptions)
print('Opening Chrome')
driver.get("http://www.jasmiths.com/data_project")
print("Downloading Data Files")
for i in range(len(files)):
WebDriverWait(driver,waiting).until(EC.element_to_be_clickable((By.XPATH,"""/html/body/main/div/ol/li[{}]/a""".format(i+1)))).click()
time.sleep(waiting)
print("Quitting Chrome")
driver.quit()