samedi 14 décembre 2019

How to check if element is displayed on website?

I'm trying to create a login test so the 1st step is that if the user successfuly logs in the script should look for an element in the home page post login.

My problem is that if the user unable to login python throws a NoSuchElement exception and does not go to else.

    prof_icon= driver.find_element_by_xpath("//button[contains(@class,'button')]")
    if prof_icon.is_displayed():
        print("Success: logged in!")
    else :
        print("Failure: Unable to login!")

I have also tried:

 prof_icon= driver.find_element_by_xpath("//button[contains(@class,'button')]")
 try:   
      if prof_icon.is_displayed():
        print("Success: logged in!")
 except NoSuchElement :
        print("Failure: Unable to login")

But the script always crashes and throws exception. I just need it to print the message in else incase the element is not displayed.

Aucun commentaire:

Enregistrer un commentaire