How to create a Selenium Webdriver test to verify no item?

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

I have an AJAX method that removes a topic from a database. I am trying to figure out how to verify that a deleted topic is no longer present on the page.

I am trying to catch an exception that should be thrown when Webdriver cannot find the element: selenium.common.exceptions.NoSuchAttributeException

Instead, I see an error:

*** URLError: <urlopen error timed out> 

Here is how I installed 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 check if a page element is missing?

+4
source share
10 answers

Except that this may not work, because if nothing is found,

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

will throw a NoSuchElementException, which is probably the Java equivalent for the exception you see.

Not sure about Python, but in Java it will be better

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

or vice versa

 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 )

+4
source
 from selenium.common.exceptions import NoSuchElementException with self.assertRaises(NoSuchElementException): self._driver.find_element_by_link_text("Topic to delete") 
+4
source

You get the wrong exception. When an item is not found, it raises a NoSuchElementException

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

since the item is removed by an AJAX call, I would check it like this:

 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 cause the webdriver to wait 10 seconds until the element is there. You can increase the wait time for more than 10 seconds if you wish.

+1
source

In Java, it worked for me:

 try { assertNull(findElement(By.cssSelector("..."))); } catch (NoSuchElementException e) { // its OK, as we expect this element not to be present on the site } 
+1
source
 element_you_are_looking_for = driver.find_elements_by_name("elements name") #notice how it says "ELEMENTSSSSSSSSSSSSSSSSS" if len(element_you_are_looking_for): print("element is present") elif: print("element is not present") 

or you can just do

 if not len(element_you_are_looking_for): print("Element Not present!") 

This works because if you find_elementSSSSSSSS___XXXX (elements with S and xxxx = name, tagname, xpath, watever) and nothing exists, it just returns an empty list.

+1
source

In Java you will do:

 assertNotNull(driver.findElement(By.css("<css selector>"))); 
0
source

Your code looks like it is Python. Since your theme is of type LINK_TEXT, something like this should work:

  for i in range(60): try: if not self.is_element_present(By.LINK_TEXT, "Topic to delete"): break except: pass time.sleep(1) else: self.fail("time out") 

If it is a different type (i.e. XPATH), you can use the same method, but instead of the name of the link text you should specify the location of the element on your page.

0
source

I think it can be defined to detect a visible element, only for django

 from selenium.webdriver.chrome.webdriver import WebDriver from django.test import LiveServerTestCase class MyLiveServerTestCase(LiveServerTestCase): def test_element_not_displayed(self): self.selenium.get('http://your_url.com') displayed = self.selenium.find_element_by_tag_name('table').is_displayed() self.assertFalse(displayed) 
0
source

Very late, because most of the answers are already here for Java. The question is python. Many python answers are syntactically incorrect. The correct form would include:

 selenium.common.exceptions import NoSuchElementException # other code self.assertRaises(NoSuchElementException, self._driver.find_element_by_link_text, "Topic to delete") 

This last line is mainly the source code of OPs, but is rewritten in such a way as to ensure that the statement is executed without exception.

0
source

Source: https://habr.com/ru/post/1394279/


All Articles