Getting all the elements in DOM tree

49 views
Skip to first unread message

Anita Kar

unread,
Mar 26, 2015, 8:38:01 AM3/26/15
to seleniu...@googlegroups.com
I am working on a project where I need to get the list of all the web elements in the DOM tree so that I can examine the attributes of each of them. I am aware of findElement(By ...) but that only works when you specify a "By" condition. I however, need the list of all the elements in the tree. Please let me know if there is a way to do so. 

Krishnan Mahadevan

unread,
Mar 26, 2015, 8:46:53 AM3/26/15
to Selenium Users
First use driver.getPageSource() and then use the page source with a library such as this ? http://htmlparser.sourceforge.net/

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

On Thu, Mar 26, 2015 at 11:08 AM, Anita Kar <anita...@gmail.com> wrote:
I am working on a project where I need to get the list of all the web elements in the DOM tree so that I can examine the attributes of each of them. I am aware of findElement(By ...) but that only works when you specify a "By" condition. I however, need the list of all the elements in the tree. Please let me know if there is a way to do so. 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c205e5d0-a1da-4db1-a97d-29a9b9dafaf7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Baswaraj Malage

unread,
Nov 21, 2016, 4:10:15 AM11/21/16
to Selenium Users
Krishnan
I ran into an issue of identifying all DOM elements within a div. When I look at pagesource, looks like it return entire page. In my example, I have two <div>s with unique IDs but elements within those divs are having same class, css, tag name etc... so, there is no way I can identify those elements on the page.
BTW, I have tried getting all elements with same class then identify using index i.e. [0], [1] etc... but this will not work for our actual project and there seems to be very high maintenance.

Is there a way limit page source to specific div?

-Baswaraj

praveen kumar cherukuri

unread,
Nov 21, 2016, 4:18:44 AM11/21/16
to seleniu...@googlegroups.com
Hi, 

We can add a filter to the element search we do on the DOM. 

Example: 
- This will return all the div elements with an 'id' attribute from the dom. : //div[@id]
- This will return all the div elements with an 'id' with value as 'abc' attribute from the dom. : //div[@id='abc']
- This will return all the div elements with an 'id' or 'class' attribute : "//div[@id or @class]"
- This will return all the div elements with an 'id' or 'class' with specific value : "//div[contains(@id,'cond') or contains(@class,'cell')]"


Let us know if your requirement is addressed or not. 
Or please be more detailed with small code snippet and example with expected behavior. 

Cheer., 
Praveen. 


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c7fee5fb-ac50-4827-8b2e-03b9e4aec2e2%40googlegroups.com.

Baswaraj Malage

unread,
Nov 21, 2016, 7:51:19 AM11/21/16
to Selenium Users
Thanks for your suggestions. Just to clarify, when I say <div> it's not table. It's a div wrapping many html elemnts.
I am attaching a simplified example with two divs having unique IDs but "Table A", "Table B" text and table(s) share same class name. Let us say that class name is "html-text".

I'm looking for methods to verify "Table A" text has been displayed within <div id = "d12345">. In other words, I would like to navigate to html elements within that div using it as starting point, so that I don't have to read all html elements with class-name as "html-text" and then use index [0] or xpath as actual web page more complex.

Thanks
Baswaraj

Div Problem.JPG
Reply all
Reply to author
Forward
0 new messages