Solved

Processing the HTML DOM in Javascript with XPath

Posted on 2009-05-06
7
539 Views
Last Modified: 2013-11-19
I need to do some test in my code, so I'm interested in processing the HTML DOM in a IFrame being tested. Some complex XPath can allow me to find elements ease.

So lets see the questions.

Having a document, II need to use some XPath to find Elements in the HTML DOM.
How to do it?
0
Comment
Question by:oleber
  • 3
  • 2
7 Comments
 
LVL 10

Expert Comment

by:bugada
ID: 24312235
I suggest you to read this tutorial, paying attention to the examples given.

http://www.w3schools.com/XPath/default.asp
0
 
LVL 10

Author Comment

by:oleber
ID: 24312358
My question is not how to write XPath, I know that.
The idea is to get my application in a Iframe. After, to be able of finding elements in the HTML DOM of that IFrame using XPath.

NOTE: The application and the Test pages are in the some domain.

Let me rephrase my questions:

How do I get the DOM of the Iframe?
How to find a element or a group of elements in the DOM using XPath.

0
 
LVL 10

Accepted Solution

by:
bugada earned 500 total points
ID: 24312417
if the pages are in te same domain, supposing your iframe has an id="IF", in the container page you can access the iframe document like this:

var iframeDocument = document.getElementById("IF").document;

and then use xpath as usually. If you don't know how to use xpath by javascript read this

http://www.w3schools.com/XPath/xpath_examples.asp



0
 
LVL 10

Author Comment

by:oleber
ID: 24312527

I have this code, the first 2 alerts appear.

The 3º doesn't appear.

What is wrong ???
<html>
    <script>
        function myTest(iframe) {
            alert("Loaded: " + iframe );
            var document = iframe.contentDocument;
            if ( document == null || document == undefined ) {
                document = iframe.contentWindow.document;
            }
 
            alert("Doc: " + document );
            
            var elem = document.selectNodes("//body");
            alert("XPath: " + elem );
        }
    </script>
    <body bgcolor="#FF0000">
        <iframe id="RSIFrame" name="RSIFrame" style="width:800px; height:600px; border: 3px" src="iframe.html" onLoad="myTest(this)">
        </iframe>
    </body>
</html>

Open in new window

0
 
LVL 10

Assisted Solution

by:bugada
bugada earned 500 total points
ID: 24312632
selectNodes is not supported except than on older version of IE.
Try to substitute it with this calll
var elem = document.evaluate("//body", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question