Solved

Processing the HTML DOM in Javascript with XPath

Posted on 2009-05-06
7
536 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
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 …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now