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()

Erki M.
  • 5,022
  • 1
  • 48
  • 74
Harshavardhan Konakanchi
  • 4,238
  • 6
  • 36
  • 54

2 Answers2

22

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!
Sachin Mhetre
  • 4,465
  • 10
  • 43
  • 68
  • 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 ...}`. – Ross Patterson Apr 19 '12 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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 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 Sep 10 '13 at 14:45