Element not found blocks execution in Selenium

Posted by Mariano on Stack Overflow See other posts from Stack Overflow or by Mariano
Published on 2012-11-23T22:51:20Z Indexed on 2012/11/23 23:04 UTC
Read the original article Hit count: 179

Filed under:
|
|

In my test, I try to verify if certain text exists (after an action) using find_element_by_xpath. If I use the right expression and my test pass, the routine ends correctly in no time. However if I try a wrong text (meaning that the test will fail) it hangs forever and I have to kill the script otherwise it does not end.

Here is my test (the expression Thx user, client or password you entered is incorrect does not exist in the system, no matter what the user does):

# -*- coding: utf-8 -*-                                                         
import gettext                                                                  
import unittest                                                                 

from selenium import webdriver                                                  

class TestWrongLogin(unittest.TestCase):                                        
    def setUp(self):                                                            
        self.driver = webdriver.Firefox()                                       
        self.driver.get("http://10.23.1.104:8888/")                             
        # let's check the language                                              
        try:                                                                    
            self.lang = self.driver.execute_script("return navigator.language;")
            self.lang = self.lang("-")[0]                                       
        except:                                                                 
            self.lang = "en"                                                    
        language = gettext.translation('app', '/app/locale', [self.lang],          
                                       fallback=True)                           
        language.install()                                                      
        self._ = gettext.gettext                                                

    def tearDown(self):                                                         
        self.driver.quit()                                                      

    def test_wrong_client(self):                                                
        # test wrong client                                                     
        inputElement = self.driver.find_element_by_name("login")                
        inputElement.send_keys("root")                                          
        inputElement = self.driver.find_element_by_name("client")               
        inputElement.send_keys("Unleash")                                        
        inputElement = self.driver.find_element_by_name("password")             
        inputElement.send_keys("qwerty")                          
        self.driver.find_element_by_name("form.submitted").click()              
        # wait for the db answer                                                
        self.driver.implicitly_wait(10)                                         
        ret = self.driver.find_element_by_xpath(                                
            "//*[contains(.,'{0}')]".\                                          
            format(self._(u"Thx user, client or password you entered is incorrect")))
        self.assertTrue(isinstance(ret, webdriver.remote.webelement.WebElement))

if __name__ == '__main__':                                                      
unittest.main()   

Why does it do that and how can I prevent it?

© Stack Overflow or respective owner

Related posts about python

Related posts about unit-testing