4

I'm using Selenium to do Java test. I know that i can check if an element is enable or displayed with this :

 isDisplayed
 isEnabled

Is it possible to check if an element is not visible, in the case that this element is under another ? For example, if a div is under another one. With that i want to check GUI element. For example if a button move under element, etc ...

Any ideas ?

thanks for helping _

MxfrdA
  • 171
  • 3
  • 14
  • 1
    You mean visually under another element or do you mean a child/descendant of another element? It sounds like you mean visually under... e.g., is element X under a popup. – JeffC Mar 14 '16 at 18:58
  • In my case it's when it's visually under. It can be a div under a pop-up but i was thinking of Css modification that can hide an element. For example a big div hidding a button – MxfrdA Mar 15 '16 at 07:56

4 Answers4

1

selenium.isElementPresent() or selenium.isVisible()

those may help you.

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!

Y.Kaan Yılmaz
  • 612
  • 5
  • 18
  • 1
    My problem is that, in my case, the element is already on the page, but hidden by an other element. isElementPresent() can't check that ? – MxfrdA Mar 14 '16 at 14:40
  • if element is on the page, selenium can reach. Just try to reach it like driver.findElement(By.id("id")); – Y.Kaan Yılmaz Mar 14 '16 at 14:41
  • 1
    Ok, and if it's hidden by an another element it can't detect it ? – MxfrdA Mar 14 '16 at 14:43
  • What do you mean "hidden by another element" ? – Y.Kaan Yılmaz Mar 14 '16 at 14:43
  • For example, a div in position:absolute; that hide another div – MxfrdA Mar 14 '16 at 14:45
  • Yes, selenim can detect. Actually you can as well just hit f12 and console(in my case, I'm using Chrome) you can track down "hidden" (as you say) elements and reach their context or html properties. – Y.Kaan Yılmaz Mar 14 '16 at 14:47
  • How do you track down " hidden " ? – MxfrdA Mar 14 '16 at 14:50
  • you said a div(with id hider) that hide another div(with id hidden).you can search in Elements(when you press f12 in browser) Thus, you can find by looking(manually). Hidden elements are just not visible to "end user", they can be seen in Elements(if you are using Chrome). – Y.Kaan Yılmaz Mar 14 '16 at 15:02
  • 3
    Yes that's it, but i'm looking for a way to automate this :p – MxfrdA Mar 14 '16 at 15:11
  • Selenium can do exactly what you can do with browser. Just use driver.findelement(By.id("id")); Since you manually find "hidden" elements ids :) now you can give its id to selenium to do whatever your needs. – Y.Kaan Yılmaz Mar 14 '16 at 15:28
  • 2
    Actually, this answer is off-topic. The quesion is about how to detect if something cannot be clicked or intreacted with, because it's covered by another element. – KajMagnus Jan 25 '19 at 14:33
1

I think constructing your locator like this might do it.

By TOP = By.xpath(".//div");
By UNDER = By.xpath("..//..//div");
By elementUNDERtheTOP = new ByChained(TOP, UNDER);

This is the equivilant of:

driver.findElement(TOP).findElement(UNDER);

This is possible to do with XPath but I don't think you could do this with a CSS locator because a CSS locator would not be able to traverse up the DOM tree to parent elements.

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • This is exactly what he needs, but hope it wil not confuse him more. +1 for sample code. – Y.Kaan Yılmaz Mar 14 '16 at 15:03
  • That's it ! What does this ByChained returns ? How can i integrate it ? Thanks for help – MxfrdA Mar 14 '16 at 15:11
  • Ok, I clarified it a bit to make it less confusing. – djangofan Mar 14 '16 at 19:25
  • 4
    Actually, this answer is off-topic. The quesion is about how to detect if something cannot be clicked or intreacted with, because it's covered by another element. (Not nested within it, but occluded by it.) – KajMagnus Jan 25 '19 at 14:33
1

Lets say you want to click an element, but that element is "behind"/"below" (aka 'obstructed' by anlother element), Selenium will actually throw a ElementClickIntercepted exception. As part of the exception message, it also returns which point you tried to click and which other element (tag, class) did intercept the click. By simply attemping to click an element and specifically catching this type of exception, you get the information you're looking for, if you catch such an exception => element not clickable (at the given coordinates - in most cases the default click point (or if you use actions: the offset you define)), if no exception => element is clickable.

1

The solution for Python is here.

It should be easy to convert it to Java.

eNca
  • 1,043
  • 11
  • 21