1

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 NoSuchElementException exception and does not go to else.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

def login_test(self):
    driver_location = 'D:\\chromedriver.exe'
    os.environ["webdriver.chrome.driver"] = driver_location
    driver = webdriver.Chrome(driver_location)
    driver.maximize_window()
    driver.implicitly_wait(3)
    driver.get("http://www.example.com/")

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 NoSuchElementException :
        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.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Ryan
  • 93
  • 7
  • Could you share the code above prof_icon= driver.find_element_by_xpath("//button[contains(@class,'button')]") and html fot it ? – Iwona Dec 14 '19 at 17:24
  • I have added the code above prof_icon. Thank you – Ryan Dec 14 '19 at 17:44

2 Answers2

0

It should be:

except NoSuchElementException:  #correct name of exception
    print("Failure: Unable to login")

You can see if element exist and if not print "Failure: Unable to login". Note the plural "s" in .find_elements_*.

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

Hope it helps!

Iwona
  • 83
  • 1
  • 5
0

You were close. To locate the element you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    try:   
        WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.button")))
        print("Success: logged in!")
    except TimeoutException:
        print("Failure: Unable to login")
    
  • Using XPATH:

    try:   
        WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[contains(@class,'button')]")))
        print("Success: logged in!")
    except TimeoutException:
        print("Failure: Unable to login")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • @Ryan [Upvote](https://stackoverflow.com/help/why-vote) the answer if this/any answer is/was helpful to you for the benefit of the future readers. – DebanjanB Dec 20 '19 at 14:58