Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If else
#1
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
Reply
#2
Ok? What problem are you having? All I see are syntax problems: missing colons and no indentation.
Reply
#3
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]
Reply
#4
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.
Reply
#5
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} ]")
Reply
#6
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?
Reply
#7
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()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020