AlphaFoxtrot
AlphaFoxtrot

Reputation: 684

How to assert text is not present for a specific element in python/selenium?

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'.

Upvotes: 4

Views: 2381

Answers (1)

Mahsum Akbas
Mahsum Akbas

Reputation: 1583

self.assertNotEqual(str(elem.text), "Your email address or password is invalid.", "your assertion fail message-optional")

Upvotes: 1

Related Questions