If else - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: If else (/thread-33328.html) |
If else - africanism - Apr-16-2021 Hello everyone, I'm new to python and I'm blocking on a piece of code, basically I would like that in the if condition, if the condition is true it executes otherwise it goes on. if driver.find_element_by_xpath(f"//*[@title={date} ]") dossier = driver.find_element_by_xpath(f"//*[@title={date} ]").click() download= driver.find_element_by_xpath("//span[contains(.,'Télécharger')]").click() driver.close() driver.quit() else driver.close() driver.quit()Thanks for your help RE: If else - ndc85430 - Apr-16-2021 Ok? What problem are you having? All I see are syntax problems: missing colons and no indentation. RE: If else - africanism - Apr-16-2021 thank you for your answer, my concern is that I am not sure of my IF condition. if I execute I have this error selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: // * [@ title = 16042021] RE: If else - ndc85430 - Apr-16-2021 If that method throws when it can't find the thing you're looking for, then you need to handle the exception with try and except . You can of course wrap the exception handling in a function to return a value if you wanted and test that.
RE: If else - africanism - Apr-16-2021 i tested with try but i got an error, sorry i'm new to python. try: dossier = driver.find_element_by_xpath(f"//*[@title={date} ]") download= driver.find_element_by_xpath("//span[contains(.,'Télécharger')]").click() driver.close() driver.quit() except: driver.close() driver.quit()File "c:\Users\Daniel\Desktop\voxaly.py", line 35 dossier = driver.find_element_by_xpath(f"//*[@title={date} ]") RE: If else - ndc85430 - Apr-16-2021 You haven't shown what the error is and in any case, there's no indentation. Perhaps you need to spend some time learning the language before doing a project? RE: If else - africanism - Apr-16-2021 i was successful after some research you suggested to me, here is my code i hope it is correct. #Download try: dossier = driver.find_element_by_xpath(f"//*[@title={date} ]") dossier.click() download = driver.find_element_by_xpath("//span[contains(.,'Télécharger')]") download.click() driver.close() driver.quit() except NoSuchElementException: #Erreur lors de l'execution print ("Pas de fichier") driver.close() driver.quit() |