How do I create a Selenium Webdriver test to verify an element is not present?

10,924

Solution 1

from selenium.common.exceptions import NoSuchElementException

with self.assertRaises(NoSuchElementException):
    self._driver.find_element_by_link_text("Topic to delete")

Solution 2

Except that might not work because if nothing is found,

driver.findElement(By.css("<css selector>")) 

will return an NoSuchElementException which is probably the Java equivalent of the exception you are seeing.

Not sure about Python but in Java this would be better

assertTrue(driver.findElements(By.whateverYouWant).size() == 0) 

or in reverse

assertFalse(driver.findElements(By.whateverYouWant).size() > 0)

http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.html#findElement(org.openqa.selenium.By)

Solution 3

You're catching incorrect exception. When element is not found it raises NoSuchElementException

self.assertRaises('selenium.common.exceptions.NoSuchElementException', self._driver.find_element_by_link_text("Topic to delete"))

Solution 4

since the element is being deleted by an AJAX call, I would check it like so:

from selenium.webdriver.support.ui import WebDriverWait

w = WebDriverWait(driver, 10)
w.until_not(lambda x: x.find_element_by_link_text("Topic to delete"))

This will make webdriver wait for 10 seconds until the element is NOT there. You can increase the timeout to longer than 10 seconds if you like.

Solution 5

In Java what worked for me was:

try {
    assertNull(findElement(By.cssSelector("...")));
} catch (NoSuchElementException e) {
    // its OK, as we expect this element not to be present on the site
}
Share:
10,924
BryanWheelock
Author by

BryanWheelock

GoogleAppEngine and Python programmer I believe all suffering is caused by attachments. The only constant is Change.

Updated on June 12, 2022

Comments

  • BryanWheelock
    BryanWheelock almost 2 years

    I am creating unit tests for my Django app in Selenium Webdriver.

    I have an AJAX method that deletes a Topic from the database. I'm trying to figure out how to verify that the deleted Topic is no longer present on the page.

    I am trying to catch the Exception that the should be generated when Webdriver can't find an element: selenium.common.exceptions.NoSuchAttributeException

    Instead I see an error:

    *** URLError: <urlopen error timed out>
    

    Here is how I have setup the tests:

    from selenium import webdriver
    from django.utils import unittest  
    
    class TestAuthentication(unittest.TestCase):    
        scheme = 'http'    
        host = 'localhost'    
        port = '4444'    
    
    
        def setUp(self):    
            self._driver = webdriver.Firefox()    
            self._driver.implicitly_wait(10)    
    
        def login_as_Kallie(self):    
            # Kallie has manual login    
            self._driver.get('http://localhost:8000/account/signin/')    
            user = self._driver.find_element_by_id('id_username')    
            user.send_keys("Kallie")    
            password = self._driver.find_element_by_id('id_password')    
            password.send_keys('*********')    
            submit = self._driver.find_element_by_id('blogin')    
            submit.click()    
    
        def test_Kallie_can_delete_topic(self):    
            self.login_as_Kallie()    
            self._driver.find_element_by_link_text("Topic to delete").click()    
            self._driver.find_element_by_xpath("/html/body/div/div[4]/div/div/div[2]/div/table/tbody/tr/td[2]/div/div[3]/span[5]/a").click()    
            self._driver.find_element_by_class_name('dialog-yes').click()    
            self._driver.get("http://localhost:8000/questions/")    
            # this is the Python exception to catch: selenium.common.exceptions.NoSuchAttributeException    
            self.assertRaises('selenium.common.exceptions.NoSuchAttributeException', self._driver.find_element_by_link_text("Topic to delete"))    
    
        def tearDown(self):    
            self._driver.quit()    
    

    How can I test that a Element of the page is absent?

  • BryanWheelock
    BryanWheelock about 12 years
    The only exception I'm getting is: URLError: <urlopen error timed out>
  • e4c5
    e4c5 over 8 years
    This is java code while the question is for python and django
  • e4c5
    e4c5 over 8 years
    This is java code while the question is for python and django
  • Artjom B.
    Artjom B. over 8 years
    You probably mean semantic instead of syntax, because the syntax is fine in every answer.