Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Processing the HTML DOM in Javascript with XPath

Posted on 2009-05-06
7
Medium Priority
?
554 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
5 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 2000 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…
Suggested Courses

782 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