0

Using Selenium, what is the best way (most reliable) to assert that an element is not present on the page?

We give the element an id:

<div id="12345"></div>

in our Selenium code, we have something like this:

      let el = await driver.findElement(By.id(`12345`));
      await driver.wait(until.elementIsVisible(el), 6000).then(function () {
          // we would fail the test here
          throw new Error('element was not supposed to be found!')
      });

should we simply expect a timeout when searching for the element? Or is there a better way than expecting a timeout?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    Possibly being nitpicky here, don't mean to offend. Is the issue asserting the element's existence/visibility, or creating a robust usage of `isVisible`? [IsVisible](https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_Selenium_DefaultSelenium_IsVisible.htm) "An element can be rendered invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if its ancestors". I've heard a few varying definitions for "element is not present" and wanted to call out that possible pitfall. – Hodrobond Jun 05 '17 at 20:22
  • yeah, good point. I totally mean to check either that the element is not present on the page at all *or* if the element has the hidden property, either one. Perhaps demonstrating a separate check for each would be good. – Alexander Mills Jun 05 '17 at 20:26
  • I actually think the code that I have above, would satisfy what I am looking for - the element will not be visible if it's either not present in the page at all or if it has the hidden attribute. – Alexander Mills Jun 05 '17 at 20:27
  • is that correct or am I missing something? – Alexander Mills Jun 05 '17 at 20:28
  • Yeah you sound to be in line with the definition for isVisible – Hodrobond Jun 05 '17 at 20:29
  • do you happen to know if there is a Selenium check to assert that the element with the id is not on the page at all? – Alexander Mills Jun 05 '17 at 20:42
  • 2
    Sorry, was in a meeting. [This question](https://stackoverflow.com/questions/10223157/difference-between-iselementpresent-and-isvisible-in-selenium-rc) might be helpful! There is an `isElementPresent`, you might want to use a combination of both =) – Hodrobond Jun 05 '17 at 21:10
  • 1
    I usually make sure that page has been loaded correctly, then do wait for element, but reduce wait timeout to as minimum as possible, thus will not hurt performance. whether using element Present or visible will depend on whether you need to make sure that it is visible in viewport or not – adee alamsz Jun 06 '17 at 06:59

1 Answers1

0

You should utilize a combination of WebDriverWait and the ExpectedCondition utilities. The WebDriverWait establishes the time it will wait for the element to not be visible. In the example below, it is set to 10 seconds, but you can adjust this to whatever works for you. The ExpectedCondition, particularly the invisibility_of_element_located() method is what you would use. So something like this is what you need (in Python):

WebDriverWait(driver, 10).until(ExpectedCondition.invisibility_of_element_located((By.ID, elementIdentifier)))
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817