vendredi 3 avril 2020

Add more then one Python Selenium WebDriverWait exception to download an element

So I have this in my script:

today = datetime.now()
today = today.strftime("%Y%m%d")
yesterday = datetime.now() - timedelta(days=1)
yesterday = yesterday.strftime("%Y%m%d")
y_yesterday = datetime.now() - timedelta(days=2)
y_yesterday = y_yesterday.strftime("%Y%m%d")
y2_yesterday = datetime.now() - timedelta(days=3)
y2_yesterday = y2_yesterday.strftime("%Y%m%d")
y3_yesterday = datetime.now() - timedelta(days=4)
y3_yesterday = y3_yesterday.strftime("%Y%m%d")
exp = "//*[@title='Some_excel_to_download_"+today+".xls']"
exp_y = "//*[@title='Some_excel_to_download_"+yesterday+".xls']"
exp_y2 = "//*[@title='Some_excel_to_download_"+y_yesterday+".xls']"
exp_y3 = "//*[@title='Some_excel_to_download_"+y2_yesterday+".xls']"

Basicaly, I want to download an xls file from the web site. I want to automate this process using crontab scheduler. Problem is, that the file I want to download is uploaded at random dates and not event at random hour, but sometimes they forget to upload it for even 2 or 3 days (automation vs. manual job :) )

For now I am using this:

try:
    WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH,("%s" % exp)))).click()
    nazwa = 'Some_excel_to_download_'+today+'.xls'
    print(nazwa)
except TimeoutException:
    print("Today's file not found. Trying: " + exp_y)
    exp = "//*[@title='Some_excel_to_download_"+yesterday+".xls']"
    driver.find_element_by_xpath(exp_y).click()
    nazwa = 'Some_excel_to_download_'+yesterday+'.xls'
    print(nazwa)

and this works fine, as long as a xls file is one day old...I want to know, how to add more "except" statements or (I guess it's better option) something like "if, elif, else" statement, but I am not sure how to do it... Maybe you could help me there, please ?

Aucun commentaire:

Enregistrer un commentaire