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
Ok? What problem are you having? All I see are syntax problems: missing colons and no indentation.
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]
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.
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} ]")
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?
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()