Solved

Processing the HTML DOM in Javascript with XPath

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

623 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