Skip to main content
2 of 2
Fixed code formatting and the exception name

is_displayed with python and web driver

So when my test pass it will show the "yeah" word in the end, however when it fails , it will only show and exception NoSuchElementException and the word "Failed" will not show up anywhere. its like else statement is not there. What should i do to make the word Failed show up when the test fails.

class CreateAccount(unittest.TestCase):
    def setUp(self):
        global driver
        driver = webdriver.Firefox()
        driver.implicitly_wait(10)
        driver.get("http://www.copy.com")
        driver.maximize_window()


    def test_main(self):
        createNewAccount = driver.find_element_by_xpath(
            "/html/body/main/div/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div[1]/div/form/div[5]/div[1]/a")
        createNewAccount.click()

        firstName = driver.find_element_by_xpath("//*[@id='dom_id_3']")
        lastName = driver.find_element_by_xpath("//*[@id='dom_id_4']")
        emailField = driver.find_element_by_xpath("//*[@id='dom_id_5']")
        passwordField = driver.find_element_by_xpath("//*[@id='dom_id_6']")

        submitButton = driver.find_element_by_xpath(
            "/html/body/main/div/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div[2]/div[1]/form/button")

        firstName.send_keys("mike")
        lastName.send_keys("mano")
        emailField.send_keys("[email protected]")
        passwordField.send_keys("test12")

        submitButton.click()

        xpath = driver.find_element_by_xpath(
            "/html/body/main/div/article[2]/div[4]/header/div[2]/div/div[4]/a")

        **if xpath.is_displayed():
            print("yeah")
        else:
            print("Failed")**


    def tearDown(self):
        driver.quit()


if __name__ == "__name__":
    unittest.main()