10

What is the difference between element isElementPresent and isVisible in Selenium RC. I get true for

selenium.isElementPresent() and selenium.isVisible()

If I get false for selenium.isElementPresent() I get Exception on selenium.isVisible()

2 Answers 2

23

isElementPresent() - This method basically tests if the element we are looking for is present somewhere on the page.

isVisible() - looks for display: none style tag - this might throw a null pointer if we aren't careful...thus to see if an element is visible first check if the element is present using isElementPresent() method. Then try checking if the element is visible!

Observe that isElementPresent() won't mind even if our element is not visible.

For ex: lets say the below is the html code for a component on my test application:

now if you test the above component with

selenium.isElementPresent("testinput") - returns true!
selenium.isVisible("testinput") - returns false!
1
  • isVisible() won't get a null pointer exception. But you can get a SeleniumException if the element isn't present. The usual technique is something like if (selenium.isElementPresent(locator) && selenium.isVisible(locator)) { ... do something ...}. Commented Apr 19, 2012 at 17:45
12

How about reading the documentation?

boolean isElementPresent(java.lang.String locator)

Verifies that the specified element is somewhere on the page.

boolean isVisible(java.lang.String locator)

Determines if the specified element is visible. 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. This method will fail if the element is not present.

1
  • 2
    The API seems poorly designed. If you want an assertion you say assertElementIsPresent or assertIsVisible. isMethods shouldn't be throwing exceptions if the target isn't available. The question comes from the asymmetric design of the API. You must have been having a bad day that day.
    – boatcoder
    Commented Sep 10, 2013 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.