Han
Han

Reputation: 5652

How do I verify that an element does not exist in Selenium 2

In Selenium 2 I want to ensure that an element on the page that the driver has loaded does not exist. I'm including my naive implementation here.

    WebElement deleteLink = null;
    try {
        deleteLink = driver.findElement(By.className("commentEdit"));
    } catch (NoSuchElementException e) {

    }
    assertTrue(deleteLink != null);

Is there a more elegant way that basically verifies to assert that NoSuchElementException was thrown?

Upvotes: 24

Views: 86030

Answers (9)

Nam G VU
Nam G VU

Reputation: 35434

Using webdriver find_element_xxx() will raise exception in my code and take us the waiting time of implicit/explicit webdriver wait.

I will go for DOM element check

webdriver.execute_script("return document.querySelector('your css class')")

p.s.

Also found similar discussion on our QA-main sister site here


For full check for visibility+existence

    # check NOT visible the :aftermeet and :viewintro
    #                   !                .      .                       !       !offsetWidth to check visible in pure js ref. https://stackoverflow.com/a/20281623/248616
    css='yourcss'; e=wd.execute_script(f"return document.querySelector('{css}').offsetWidth > 0") ; assert not e

    # check NOT exists :viewintro
    css='yourcss'; e=wd.execute_script(f"return document.querySelector('{css}')") ; assert not e

Upvotes: 0

Sonali Das
Sonali Das

Reputation: 1026

public boolean exist(WebElement el){
   try {
      el.isDisplayed();
      return true;
   }catch (NoSuchElementException e){
      return false;
   }
}

if(exist(By.id("Locator details"))==false)

or

WebElement el= driver.findElementby(By.locator("locator details")
public boolean exists(WebElement el)
 try{
  if (el!=null){
   if (el.isDisplayed()){
     return true;

    }
   }
 }catch (NoSuchElementException e){
   return false;
 }
}

Upvotes: 0

Vedran Zenzerović
Vedran Zenzerović

Reputation: 45

Best solution

  protected boolean isElementPresent(WebElement el){
        try{
            el.isDisplayed();
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }

Upvotes: 1

png
png

Reputation: 1130

If you're using the Javascript API, you can use WebElement.findElements(). This method will return a Promise with an array of found elements. You can check the length of the array to ensure no items were found.

driver.findElements(By.css('.selector')).then(function(elements) {
  expect(elements.length).to.equal(0)
})

I'm using Chai assertion library inside the Promise's callback to expect a certain value.

Reference: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebElement.html

Upvotes: 1

mark-cs
mark-cs

Reputation: 4707

If you are testing using junit and that is the only thing you are testing you could make the test expect an exception using

@Test (expected=NoSuchElementException.class)
public void someTest() {
    driver.findElement(By.className("commentEdit"));
}

Or you could use the findElements method that returns an list of elements or an empty list if none are found (does not throw NoSuchElementException):

...
List<WebElement> deleteLinks = driver.findElements(By.className("commentEdit"));
assertTrue(deleteLinks.isEmpty());
...

or

....
assertTrue(driver.findElements(By.className("commentEdit")).isEmpty());
....

Upvotes: 43

Guy Engel
Guy Engel

Reputation: 2616

You can retrieve a list of elements by using driver.findElements("Your elements") and then search for the element. if the list doesn't contains the element you got yourself your desired behavior :)

Upvotes: 3

Shehab Mostafa
Shehab Mostafa

Reputation: 131

You can use this:

Boolean exist = driver.findElements(By.whatever(whatever)).size() == 0;

If it doesn't exist will return true.

Upvotes: 13

anonyme
anonyme

Reputation: 1

Use assertFalse :)

assertFalse(isElementPresent(By.className("commentEdit")));

Upvotes: -3

Brantley Blanchard
Brantley Blanchard

Reputation: 1218

I split out page classes so I don't have to define elements more than once. My nunit and mbunit test classes call those page classes. I haven't tried this out yet but this is how I'm thinking about doing it so I can use .exists() like I did with WatiN.

Extension Class:

public static class ExtensionMethods
{
   public static IWebElement ElementById(this IWebDriver driver, string id)
   {
      IWebElement e = null;
      try 
      {
         e = driver.FindElement(By.Id(id));
      }
      catch (NoSuchElement){}
      return e;
   }
   public static bool Exists(this IWebElement e) 
   {
      if (e == null)
         return false;  
      return true;
   }
}

Page Class:

public IWebElement SaveButton { get { try { return driver.ElementById("ctl00_m_m_body_body_cp2_btnSave")); } }

Test Class:

MyPageClass myPageClass = new MyPageClass(driver);
if (myPageClass.SaveButton.Exists())
{
   Console.WriteLine("element doesn't exist");
}

Upvotes: 4

Related Questions