5

I know I could use (driver.findElements(By.xpath("Xpath Value")).size() != 0);

However, I am using a Page Object Model, whose entire purpose is to predefine the WebElements in a separate class, so I don't have to "FindElements By" in my test classes.

Here's what I currently have

if (objPage.webElement.isEnabled()){
   System.out.println("found element");
}else{
   System.out.println("element not found");
}

However, this tries to identify the possibly non-existent WebElement. When it is not present, I get:

No Such Element" exception.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dsidler
  • 489
  • 3
  • 13
  • 23
  • 2
    Try using List when you are using the FindBy annotation for the element which might not exist? If not size will be zero. – Grasshopper Dec 13 '16 at 16:03
  • @Grasshopper It's a good answer! Why didn't you make it full-fledged instead of simple comment? – Cryptor Apr 19 '17 at 13:23
  • @Cryptor The answer by JeffC has the same logic and I think is the correct way of handling these cases. – Grasshopper Apr 19 '17 at 13:48
  • @Grasshopper Sorry if don't get something but how it can be used in _Page Object_ model where there is no _WebDriver_ element since class is initialized using _PageFactory.initElements()_ method? – Cryptor Apr 19 '17 at 14:38
  • @Cryptor Add a new question. – Grasshopper Apr 19 '17 at 15:19
  • Canonical questions are *[Test if an element is present using Selenium WebDriver](https://stackoverflow.com/questions/7991522/test-if-an-element-is-present-using-selenium-webdriver)* (32 answers. 2011) and *[WebDriver: check if an element exists?](https://stackoverflow.com/questions/6521270/webdriver-check-if-an-element-exists)* (14 answers. 2011). – Peter Mortensen Nov 15 '22 at 22:10
  • Is that the actual message? Not something like "`selenium.common.exceptions.NoSuchElementException`"? – Peter Mortensen Nov 15 '22 at 22:20

2 Answers2

10

Best practice is to do what you originally suggested. Use .findElements() and check for .size != 0, or you can also use my preference, .isEmpty(). You can create a utility function like the below to test if an element exists.

public boolean elementExists(By locator)
{
    return !driver.findElements(locator).isEmpty();
}

You could also build this into a function in your page object.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JeffC
  • 22,180
  • 5
  • 32
  • 55
-2

You can use isDisplayed() to check whether or not an element is visible. It’s trivial enough to write a method which will do what you want. Something like:

public boolean isElementDisplayed(WebElement element) {
    try {
        return element.isDisplayed();
    } catch(NoSuchElementException e) {
        return false;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cathal
  • 1,318
  • 1
  • 12
  • 19
  • 3
    A word of warning... this answer has a hidden gotcha. What if you want to confirm that an element is NOT displayed but the element does not exist? This function will return `false` and the test will pass but the element wasn't even on the page... a false positive. Test the element for existence... then test if it is enabled. – JeffC Dec 14 '16 at 01:23
  • We need to check existence of the element before checking any condition on it. – Akarsh Apr 04 '17 at 11:57