Results 1 to 3 of 3
  1. #1
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Lightbulb printing html tree with XPath

    Hi!
    Function to print tree structure of html, here it is :
    Java Code:
    	static String getTagTree(List<WebElement>  el)
    	{
    		String treeString = "";
    	
    		for(WebElement elem : el)
    		{
    			treeString += new String("<tag:"+elem.getTagName()+
    			"  text:"+elem.getText()+">\n"
    			);
    			
    			List<WebElement> currList = elem.findElements(By.xpath("*"));
    			if(currList.size() != 0)
    				treeString +=getTagTree( currList );
    
    			treeString += new String("</tag:"+elem.getTagName()+
    			">\n"
    			);
    
    		}
    		return treeString;
    	}
    piece of html i wanted to print:

    printing html tree with XPath-sitechunk.png

    String parameter function receives:
    Java Code:
    String xpath = ".//div[@id='toctitle']";
    But function returns only this strings:
    <tag:div text:Содержание>
    <tag:h2 text:Содержание>
    </tag:h2>
    </tag:div>
    So, how can i get all of the elements from html ?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: printing html tree with XPath

    This is the API description for getText:
    "
    Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.
    "
    Which explains why you get the text repeated and nothing else, once for the <div> and once for the <h2>.
    Actually, I'm surprised the text in the <span> and <a> don't come through as well, unless the CSS is hiding that lot. Not that that's entirely relevant.

    What does toString() give you on an element?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: printing html tree with XPath

    Quote Originally Posted by Tolls View Post
    This is the API description for getText:
    "
    Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.
    "
    Which explains why you get the text repeated and nothing else, once for the <div> and once for the <h2>.
    Actually, I'm surprised the text in the <span> and <a> don't come through as well, unless the CSS is hiding that lot. Not that that's entirely relevant.

    What does toString() give you on an element?
    Actually, i know getText function description and therefore wrote
    Java Code:
    List<WebElement> currList = elem.findElements(By.xpath("*"));
    It takes all sub-elements and recursively print it :
    Java Code:
            
    if(currList.size() != 0)
                treeString +=getTagTree( currList );

Similar Threads

  1. Display rich:tree in expandable mode when page loads with tree
    By rajpan in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 08-29-2012, 02:33 PM
  2. Replies: 6
    Last Post: 01-19-2012, 05:16 PM
  3. Replies: 0
    Last Post: 05-12-2010, 11:11 AM
  4. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  5. Printing Tree Structure using Swings
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 5
    Last Post: 08-30-2008, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •