4

Is there a way to assert that there is no text in a specific element? I'm trying to test a login page where an error message will pop up when you enter invalid login information and want to test that there is no text at first.

This is the HTML element that is first displayed when the page loads:

<div class="message ng-binding”/>

Then it changes to this when invalid information is entered:

<div class="message ng-binding">Your email address or password is invalid.</div>

I tried doing this:

elem = browser.find_element_by_css_selector("div.message.ng-binding")
self.assertEqual(str(elem.text), None)

But then I would get AssertionError '' != None.

Would I have to assertEqual(str(elem.text), '')?

I've read that there are assertText functions in Selenium and tried replacing assertEqual with assertText but I would get 'Selenium_test' object has no attribute 'assertText'.

1 Answer 1

1
self.assertNotEqual(str(elem.text), "Your email address or password is invalid.", "your assertion fail message-optional")
1
  • Ah, so you just made sure that at first it's not equal to what you would expect later. Thanks, this works for me. Sep 22, 2015 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.