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.