user4990011
user4990011

Reputation:

Assert Element is present or not present

i got a function which looks something like this :

def resultCheck(self, message):
    if self.driver.find_element_by_xpath("//*[contains(text(), '{}')]".format(message)):
        return True
    else: return False

And i'd like to assert that the element either exists or doesn't

self.assertFalse(self.resultCheck('Something'), 'Detailed search failed!')

The problem is that whenever i assertFalse it throws me element not found exception, how can i tackle this?

Upvotes: 0

Views: 1558

Answers (1)

Marco Cotrufo
Marco Cotrufo

Reputation: 216

You should catch NoSuchElementException to check if an element is visible:

def assertVisible(self, message):
    try:
        return self.driver.find_element_by_xpath("//*[contains(text(), '{}')]".format(message)):  
    except NoSuchElementException:
        return False
    return False    

Upvotes: 1

Related Questions