Solved

Processing the HTML DOM in Javascript with XPath

Posted on 2009-05-06
7
543 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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