I am trying to create a Selenium Web driver java code where I can find all the hidden objects on the webpage and can be accessed. I want the count of all those hidden objects. Which property I can use in the code which can identify hidden objects because when I thought of using get Element by property so that i can search for objects with hidden property, it was not there. Is there anything i am missing here. Is it really possible ? Or do we need to know the id of the element to be found ? Any Inputs will be useful.
2 Answers
What kind of hidden objects are you looking for? If it's any HTML tags with the attribute display: none;
try using
List<WebElement> elements = driver.findElements(By.cssSelector("[display=none]"));
int count = elements.size();
This uses CSS matching to find all HTML elements with the display attribute equal to none. Then get the size of the returned list with the size()
method.
-
Thanks for that and my aim here is to find all the objects that are in the web page but are hidden, could be Html elements or not. Is it feasible ? Sep 19, 2013 at 5:58
-
From my experience hidden webpage elements would have either display:none or visibility:hidden.– FrancisSep 19, 2013 at 6:02
-
1Or they could be simply hidden behind an overlay, or with ajax it is sometimes common to actually remove unused elements from the dom and re-add them when needed. As Francis noted - looking for the styles display=none and visibility:hidden will find you all of the elements that are hidden via CSS and styling. Sep 19, 2013 at 15:18
-
Hey Francis. I tried running the code you suggested both ways but i am still getting the count as 0. is there anything which needs to be rectified. I tried testing it on multiple websites but its not getting me any output. Please help. Sep 20, 2013 at 18:16
-
Try replacing display=none with visibility=hidden. If the count is still 0 then the elements aren't hidden by CSS but might be either under an overlay or removed from the DOM as Sam noted.– FrancisSep 20, 2013 at 19:23
There is also the long option which uses selenium's element displayed functionality.
W3C specifications for element displayed
Python example:
from selenium import webdriver
b = webdriver.Chome()
b.get('https://google.com')
els = b.find_elements_by_css_selector('*')
hidden_els = [el for el in els if not el.is_displayed()]
print(hidden_els)
b.quit()